Jeff
Jeff

Reputation: 1004

Using aplay to play a sound through subprocess within a python script

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"])

Upvotes: 2

Views: 8822

Answers (2)

Franco M
Franco M

Reputation: 129

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.

Upvotes: 0

PRMoureu
PRMoureu

Reputation: 13347

The syntax that will work is :

from subprocess import call
call(["aplay", "/home/pi/file.wav"])

Upvotes: 2

Related Questions