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 = "192.168.1.149"
#misty = mistyPy.Robot(ip)
volume = 100
engine = pyttsx3.init()
sentence = "This is an example"
engine.save_to_file(sentence, 'test2.mp3')
engine.runAndWait()
audioFile = "test2.mp3"

contents = []
with open(audioFile, 'rb') as fd:
    contents = fd.read()
byteArrayString = str(list(contents))
#print(byteArrayString)
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])
    else:
        new_string += (str(byte_array[i]) + ",")

#print(new_string)
print(audioFile)
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)
print(response.text)
requests.request("POST", url, data=payload, headers=headers)