I'm working on a raspberry pi script that plays a random sound clip folder and pauses the script while it is playing. I am having two issues with the following code
- The last second or two of the sound clip is sometimes cut off
- It never reaches the "Done talking" output
Any tips on how to improve this? Thanks
import time
import subprocess
import random
import os
def talk() :
print("Talking")
# Get random sound clip
soundclip = random.choice(os.listdir("/home/pi/Music/C3PO_Sounds"))
file = '/home/pi/Music/C3PO_Sounds/' + soundclip
# Determine length of clip in seconds
result = subprocess.Popen(['mplayer', '-vo', 'null', '-ao', 'null',
'-frames', '0', '-identify', file], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
idlength = [x for x in result.stdout.readlines() if b"ID_LENGTH" in x]
duration = idlength[0].split(b'=')[1].strip(b'\n')
# Play clip
cmd = "mplayer " + file
subprocess.Popen(cmd.split())
# Sleep for duration of clip
time.sleep(float(duration))
print("Done talking")