2

There are alot of libraries to play audio within a python script, I was wondering if it would be possible to simply use call aplay through the subprocess feature to play a sound? When I try it I get OSError: [Errno 2] No such file or directory but there is definitely a sound there, it works when I do it through the command prompt. I may be doing something wrong as far as syntax in the python script?

from subprocess import call
call(["aplay /home/pi/file.wav"])
Jeff
  • 1,018
  • 1
  • 15
  • 33

2 Answers2

2

The syntax that will work is :

from subprocess import call
call(["aplay", "/home/pi/file.wav"])
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • Thanks @PRMoureu! this now plays the audio, from what I was reading it must be because of having to escape the space? I will be able to accept as answer in 3 mins – Jeff Jul 17 '17 at 05:37
  • @Jeff i often have trouble with string argument, can't tell if its the space, or the `-`before options, can you share the link you've found ? – PRMoureu Jul 17 '17 at 05:45
  • 1
    Sure thing, it was the second question down mentioning escaping spaces, https://stackoverflow.com/questions/89228/calling-an-external-command-in-python#89243 They did the comma thing like you suggested in the accepted answer too but I didn't think it would was needed, but now I see it is. – Jeff Jul 17 '17 at 05:49
0

I found that installing

alsa-utils

in this case : sudo apt install alsa-utils make it work.

example of "text to speech"

import pyttsx3

# init function to get an engine instance

engine = pyttsx3.init()

# say method for input text to be spoken

engine.say('Here the message you want you hear')

# run and wait method, it processes the voice commands.

engine.runAndWait()

I hope it helps.

Franco M
  • 119
  • 1
  • 6