1

I have a Raspberry, with a simple lighttpd and python as CGI.

I try to play a MP3 to be played when I visit a web page.

So I can visit http://ip_rasp/play.py and the mp3 sounds with the Raspberry speaker.

The problem is that I think mixer is not initializing well when calling as web request:

The code is:

import pygame
import time
import os
pygame.init()
pygame.mixer.music.load("/var/www/ok.mp3")
pygame.mixer.music.play()
time.sleep(10)

It says:

 /var/www/audio/play.py in ()
      6 
      7 pygame.init()
=>    8 pygame.mixer.music.load("/var/www/ok.mp3")
      9 pygame.mixer.music.play()
     10 time.sleep(10)
pygame = <module 'pygame' from '/usr/lib/python2.7/dist-packages/pygame/__init__.pyc'>, pygame.mixer = <module 'pygame.mixer' from '/usr/lib/python2.7/dist-packages/pygame/mixer.so'>, pygame.mixer.music = <module 'pygame.mixer_music' from '/usr/lib/python2.7/dist-packages/pygame/mixer_music.so'>, pygame.mixer.music.load = <built-in function load>
<class 'pygame.error'>: mixer system not initialized 
      args = ('mixer system not initialized',) 
      message = 'mixer system not initialized'

If I run through raspberry console the command "python play.py" it works perfectly and I hear the MP3 with the speaker.

But I can not play with web request ...

Any idea or other solution ?

THANK YOU

1 Answer 1

5

I believe that the pygame.init() call is failing due to the web process not having permission to use the sound output device /dev/fb0.

According the pygame documentation, pygame.init() will not throw errors if it fails to load up a module. Since you seem to only be using the mixer module, I would suggest calling pygame.mixer.init() instead as this will report an error (you may have to call pygame.mixer.pre_init() before calling pygame.mixer.init()). If you get an error stating that permission is denied for /dev/fb0, you will need to add the www-data user to the video group (the device /dev/fb0 belongs to group video).

You can add user www-data to the group video using the following command:

sudo usermod -a -G video www-data

Hope this helps.

4
  • I added the www-data user to video, and I added the pygame.mixer.init() and now I have the error: <class 'pygame.error'>: No available audio device args = ('No available audio device',) message = 'No available audio device'
    – FlamingMoe
    Jul 25, 2015 at 11:10
  • @FlamingMoe, now that you have updated permissions, try going back to your original pygame.init() call and see what happens. The documentation says that this call does some special stuff behind the scenes to ensure that modules are loaded in the right order.
    – jswett33
    Jul 25, 2015 at 13:50
  • Is there anyway to play the whole MP3 ? I had to put "time.sleep(10)" to cheat it ... is there any way to "send the play action" to other thing in the system and the webpage just responds "ok" ?
    – FlamingMoe
    Jul 25, 2015 at 14:51
  • 1
    @FlamingMoe, I have never used the pygame library before, but from what you are describing, it sounds like the music is killed when the script finishes. What you could do to get around this is write another python script that gets run in the web request that kicks off the script you provided in your question. The script that gets run in the web request would have to run the other script in the background. To make the whole audio file play in the background script, do something like this.
    – jswett33
    Jul 25, 2015 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.