Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8044

Python • Re: Play audio on Boot on headless setup

$
0
0
in other cases this is the full code I am doing here I am using paplay but no success I got the
print("Failed to play the audio file.")
so I want to explore other options

Code:

    def play_wave_file(self, filename):        """Play the recorded wave file."""        try:            subprocess.run(["/usr/bin/paplay", filename], check=True)            #print("Playback finished.")        except subprocess.CalledProcessError:            print("Failed to play the audio file.")

It's another case of knowing that it failed without knowing why. As with any other troubleshooting you need the why or you're just guessing. And as I've already stated, I don't guess.

Try changing that function to the following and see if it sheds some light on things:

Code:

    def play_wave_file(self, filename):        """Play the recorded wave file."""        try:            result = subprocess.run(["/usr/bin/paplay", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)            result.check_returncode()        except subprocess.CalledProcessError:            print("Failed to play the audio file.")            print(result.stdout.decode())
That tells subprocess to capture both output streams (stdout and stderr) and prints them should the command fail. See the docs for subprocess.run() and subprocess.CompletedProcess

You need .decode() because the data returned will almost certainly be a byte string not a "normal" python string.

Statistics: Posted by thagrol — Mon May 06, 2024 11:44 pm



Viewing all articles
Browse latest Browse all 8044

Trending Articles