0

I'm sorry if this has been answered, I looked and cannot find the solution.

So I'm trying to play a sound only once from a raspberry pi using pythons pygame library. The raspi is hooked up via hdmi and sound is configured correctly. My original sound file was created and exported from Adobe Audition. I tried to export from audacity with the same results. I've even tried different formats. I also downloaded someone else's example with audio files and I get the same results. I've tried pre_init() with various settings, nothing here effected the non-working examples below.

This code plays the file fine, but it repeats over and over:

#!/usr/bin/env python

import pygame.mixer
import os

from pygame.mixer import Sound
pygame.mixer.quit() #Sound does not play without this line
pygame.mixer.init()

try:
  hello = Sound(os.path.join("music","Hello.ogg"))
  while True:
    hello.play()

except:
  raise UserWarning, "could not load sound file"

I've tried the below but when I run it, it pauses for a second and acts like it ran fine

#!/usr/bin/env python

import pygame.mixer
import os

from pygame.mixer import Sound
pygame.mixer.quit() #Sound does not play without this line
pygame.mixer.init()

try:
    hello = Sound(os.path.join("music","Hello.ogg"))
    hello.play(0)

except:
    raise UserWarning, "could not load sound file"

and I've tried:

#!/usr/bin/env python

import pygame.mixer
import os

from pygame.mixer import Sound
pygame.mixer.quit() #Sound does not play without this line
pygame.mixer.init()

try:
  hello = Sound(os.path.join("music","Hello.ogg"))
  i = 0
  while (i < 3):
      hello.play()
      i = i + 1
except:
  raise UserWarning, "could not load sound file"

Can you decipher from these previous examples why I can't get this to work?

1

Ok, I'm not sure if this is the proper way to fix this, but I found that the script ended before the audio has a chance to play. So to fix that I added:

time.sleep(hello.get_length())

to the script and it works fine now. So the full script looks like so:

#!/usr/bin/python
import time
import pygame
import os

pygame_init()
sound = pygame.mixer.Sound(os.path.join('music','Hello.ogg'))
sound.play()
time.sleep(sound.get_length())
0

Pythonic Approach

If your objective is to play the sound as part of special effects (say as a part of a game) your python based approach is more appropriate.


Shell Scripting

Assuming your objective is simply to play a single file once there are much simpler ways than writing a python script

My personal favorite for playing mp3 files is mpg123 which I install using

sudo apt-get install mpg123

and to launch it

mpg123 <filename>

to suppress the verbose o/p execute like so

mpg123 -q <filename>

since mpg123 doesnt play ogg files,

I use ogg123 which is available in the vorbis-tools package

sudo apt-get install vorbis-tools

and execute like so

ogg123 -q <filename>


Several times the related question has come up - how to play a song on startup:

nano /etc/rc.local

# other initialization on top

mpg123 -q /home/pi/coolsong.mp3 &
exit 0

note the trailing & will launch the process in the background and let the boot process complete when exit 0 executes


Hybrid Approach

One another indirect way is use python to launch ogg123 or mpg123 using the subprocess module Popen like so

p = subprocess.Popen(args)

and you can either wait or poll for the process to exit.

The indirect approach is generally useful if you don't need total control over the playback but just a simple interface. Sometimes I use it to play a song for a certain duration and stop by having a timer callback terminate the process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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