Playing audio files with Python

  • I am looking for a well maintained Python library with allows me to play audio files on my Raspberry Pi using the standard audio output.

    So far I've tried several, but none of them seem to work. Although pyglet works on my regular computer fine, it causes an error on the Raspberry Pi. Is there a Python library which has been proven as easy to use?

    Proven as easy to use is kind of relative, but I posted an alternative to pygame to similar question on StackOverflow over here. Basically that alternative is vlc.py (the libVLC Python module, which is very well maintained).

    A question further down says that Pyglet works on RPi. Can you update with your error?

  • xxmbabanexx

    xxmbabanexx Correct answer

    7 years ago

    I recommend the widely popular Pygame. I may be wrong, but I believe that it is pre-installed on the Pi. You can use the Pygame Mixer Music Module to play audio files. I have included some example code below.

    Assuming that we have an audio file called myFile.wav.

    import pygame
    pygame.mixer.init()
    pygame.mixer.music.load("myFile.wav")
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy() == True:
        continue
    

    NOTE: If this fails, please go to the terminal and update your system with

    apt-get update
    apt-get upgrade
    

    and try again.

    If you have more than one sound to play (which is likely) then it's better to create a pygame.mixer.Sound object for each one, then you can keep the definitions of the sound filenames in one place.

    The is what I want, but my motherboard sound doesn't have a driver so I use a USB sound card. It plays `espeak` just fine, but not this.

  • I needed a script to play a song from thirty seconds in in the background whilst responding to other user input. I then wanted it to end the song on some event.

    I don't suppose it's particularly elegant, but I opened a pipe to a background MPlayer process.

    import subprocess
    player = subprocess.Popen(["mplayer", "song.mp3", "-ss", "30"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    

    Then, when I wanted to terminate the MPlayer process, I simply wrote "q" for quit to the pipe.

    player.stdin.write("q")
    

    Look at MPlayer documentation for all sorts of commands you can pass in this way to control playback.

    Hopefully that's somewhat helpful!

    This is really neat actually. This adafruit tutorial talks about playing sound but not stopping it.

  • Another option is to use mpg321 and invoke it from the command line.

    apt-get install mpg321
    

    Then in Python:

    import os
    
    os.system('mpg321 foo.mp3 &')
    

    Pygame is almost certainly more robust, but it depends, I suppose, on what your needs are.

    I thought about this myself, but I thought it wasn't very elegant, because it make pausing, volume control etc. a lot harder.

    This is awfully slow.

  • I would recommend pyglet rather than pygame if you don't need graphics. In my view it is simpler, more elegant, and better maintained. Then again, I expect it might be a matter of taste.

    So how do you use it?

    pyglet only seems to run when a display is available; running it remotely or as a daemon only for the purpose of playing sounds doesn't work.

  • Pygame gives you a nice and easy way to load and play sounds, on a range of channels.

  • You may use a number of external players through pipes. However, you will nearly always experience a loss of speed. There is also a problem with playback control.

    I recommend you pyaudio, it works fine.

    If you want to play compressed files as well, I'll be so free to point you to my module decoder.py. You can find it on PyPI.

    It also uses subprocess, but calls decoders instead of players. They do not use a lot of CPU, and some of them know how to use hardware acceleration. You can then use pyaudio or something to output the raw audio data. If you want, you can even use pygame by utilizing pygame.sndarray. In that way you can play more formats than pygame supports.

    I tried decoder.py in PyQT GUI environment, with pyaudio and also using aplay as an external player. That was on Raspberry Pi B.

    Results were pretty satisfying in both cases.

    The simplicity can be questioned.

    decoder.py itself is very simple, but you have to combine it with other libs and that increases code lines.

    There is an example on how to play files included with decoder.py.

  • I'm running OSMC (Debian) and I got sound to play through my TV speakers, which is connected by HDMI. https://youtu.be/p2ljmTE67gs

    My method was based on the above solution posted by @xxmbabanexx as well as here. Basically, I had to add dtparam=audio=on to /boot/config.txt

    I think the TS meant the 3.5 mm jack when referring to the "standard audio output". Does this work for the 3.5 jack as well?

    Oh I see. Hmm it probably won't, though I haven't tried it.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM