Page 1 of 1

Python music player

Posted: Thu Aug 15, 2019 8:55 am
by joshi215
Hello there,
I have got a problem with playing sounds on a raspberry pi (Model B with 26 GPIO pins) via pygame in Python. I used the following code (also tried it with a wav but with the same result):

Code: Select all

import pygame

pygame.init
s=pygame.mixer.Sound("/home/pi/Sound1.mp3") #
s.play()
But I got the following error message:
Traceback (most recent call last):
File "/home/pi/SoundPlayer_WIP.py", line 10, in <module>
s=pygame.mixer.Sound("/home/pi/Sound1.mp3")
pygame.error: Unable to open file '/home/pi/Sound1.mp3'




At the second try I used following code:

Code: Select all

import pygame
pygame.init()
pygame.mixer.music.load("Sound1.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy()==True:
    continue
    #also tried pygame.time.Clock().tick(10) with the same result.
Using a mp3 file it output a loud interferencing noise (sounds like its played 20 times faster) and returned the error message: ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred

Thanks for your help
P.S.: I hope my english is not too bad

Re: Python music player

Posted: Thu Aug 15, 2019 11:51 pm
by joelostinspace
try this example with your sound file

Code: Select all

>>> import pygame
>>> pygame.mixer.init(48000, -16, 1, 1024)
>>> sound_1= pygame.mixer.Sound('/home/pi/mysounds/circus.wav')
>>> soundChannel_1= pygame.mixer.Channel(1)
>>> soundChannel_1.play(sound_1)