0

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")
  • Are values of float(duration) so high that it may not get to that part because of the timer. – BenT Oct 23 '17 at 05:02
  • Thanks for having a look BenT - no, none of the clips are longer than 20 seconds. – zZShort_CircuitZz Oct 23 '17 at 05:35
  • Have you printed the value of `duration`, to check that it reflects what you know about the file? Wouldn't it be easier just to wait until the `mplayer` process finishes? I suspect that the duration extracted from the file might not be very accurate. – Kevin Boone Oct 23 '17 at 07:36
  • I put a print between the subprocess line and the wait, and it didn't work. I think the delay is ok, it just isn't getting past the subprocess for some reason – zZShort_CircuitZz Oct 23 '17 at 22:36

0 Answers0