Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame music doesn't work
#1
hello

I'm going through the book 'making games with pygames', but I can't make python play music. The sound works fine but not the music

Here's my code:
soundObj = pygame.mixer.Sound('beep1.ogg')
soundObj.play()
import time
time.sleep(1)
soundObj.stop()

pygame.mixer.music.load('FFT.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.stop()

while True:
	DISPLAYSURF.fill(WHITE)
	DISPLAYSURF.blit(textSurfaceObj, textRectObj)
	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit()
	pygame.display.update()
If I play WAV files I get this error:ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred
If I play MP3 files I get this: pygame.error: Unrecognized audio format
which i understand but pygame is suppose to recognize mp3 so.... (i tried playing with the name file in case i wrote FFT.mp3.mp3 but i get the same thing)
And if I play OGG files i get this: ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred

And like I said soundObj = pygame.mixer.Sound('beep1.ogg') works just fine.

Any help would be appreciated.

Thank you for your time.
Larz60+ write Dec-26-2020, 04:01 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use code tags on future posts.
Reply
#2
(Dec-25-2020, 11:59 PM)CompleteNewb Wrote: hello

I'm going through the book 'making games with pygames', but I can't make python play music. The sound works fine but not the music

Here's my code:
soundObj = pygame.mixer.Sound('beep1.ogg')
soundObj.play()
import time
time.sleep(1)
soundObj.stop()

pygame.mixer.music.load('FFT.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.stop()

while True:
	DISPLAYSURF.fill(WHITE)
	DISPLAYSURF.blit(textSurfaceObj, textRectObj)
	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit()
	pygame.display.update()
If I play WAV files I get this error:ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred
If I play MP3 files I get this: pygame.error: Unrecognized audio format
which i understand but pygame is suppose to recognize mp3 so.... (i tried playing with the name file in case i wrote FFT.mp3.mp3 but i get the same thing)
And if I play OGG files i get this: ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred

And like I said soundObj = pygame.mixer.Sound('beep1.ogg') works just fine.

Any help would be appreciated.

Thank you for your time.


This is because pygame only allows for the mixer to be used once, so if you delete sound and run it you will see that now the music plays. This is the same problem I have with a game I was working with. So you can leave the sound in and try use playsound for the music, it worked for me but it has like a delay before the file plays.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 930 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,778 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 885 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  How do I get pygame module to work in Spyder? (Mac) FirstBornAlbratross 18 4,322 Dec-18-2022, 06:43 PM
Last Post: FirstBornAlbratross
  client.get_all_tickers() Doesn't work gerald 2 1,705 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,182 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,007 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,699 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,610 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,943 Aug-10-2021, 07:00 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020