Misty Community Forum

Save Audio files - API via POST request and json in Python

HI all!

I want to play local audio files on misty via my python script. However, that is not possible, so I need to save the audio files first on the misty platform. That’s where I get stuck… Can someone please help me out? Thanks!!!

ip = ""
#misty = mistyPy.Robot(ip)
volume = 100
engine = pyttsx3.init()
sentence = "This is an example"
engine.save_to_file(sentence, 'test2.mp3')
audioFile = "test2.mp3"

contents = []
with open(audioFile, 'rb') as fd:
    contents = fd.read()
byteArrayString = str(list(contents))
h = {'Content-Type': 'multipart/form-data'}
body = '{FilenameWithoutPath: test2.mp3,DataAsByteArrayString: '+byteArrayString+', ImmediatelyApply: true, OverwriteExisting: true }'

requests.post('http://'+ip+'/api/audio', headers=h, json={"FilenameWithoutPath": audioFile, "DataAsByteArrayString": byteArrayString})

requests.post('http://'+ip+'/api/audio/play',json={"fileName": audioFile, "volume": 100})
requests.post('http://'+ip+'/api/arms',json={"arm": "both", "position": -30, "velocity": 50})

I found some help over here: How to programmatically upload and display an image to Misty I - #22 by bobh

But as you can see, I still get an error message.

with open(audio_name, "rb") as audio_file:
    byte_array = bytearray(audio_file.read())

new_string = ""
for i in range(len(byte_array)):
    if i == (len(byte_array) - 1):
        new_string += str(byte_array[i])
        new_string += (str(byte_array[i]) + ",")

url = "http://"+ip_address+"/api/audio"
payload = json.dumps({"FileName": audio_name, "DataAsByteArrayString": new_string,
                        "ImmediatelyApply": True, "OverwriteExisting": True})
headers = {
    'Content-Type': "application/json",
    'Cache-Control': "no-cache"
response = requests.request("POST", url, data=payload, headers=headers)
requests.request("POST", url, data=payload, headers=headers)