11
import pygame
pygame.mixer.init()
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

When I run the code, there's no sound and the program ends in like a second. Initially I didn't have the while loop until I saw the suggestions in the answers to similar questions. The program does enter the while loop on my friend's windows system, but not on my mac, and it doesn't have any sound either even on my friend's windows system. Does anybody know how to solve it?

emilyz
  • 121
  • 1
  • 1
  • 3
  • 2
    The above code works if you create a display screen, it does not work without it. Are you trying to play music with out a pygame screen? I could not find any documentation that shows the mixer module working stand alone from a typical pygame app. – i_4_got Nov 30 '12 at 21:17
  • I think you just need to open a window first. – ThisIsAQuestion Nov 30 '12 at 22:21

6 Answers6

17

Works well on Ubuntu 10.04 and Pygame 1.9.1.

Some things you can try:

  • initialize whole pygame pygame.init()
  • i_4_got's suggestion (create a display) pygame.display.set_mode((200,100))
  • put a pause (tick) between play an get_busy
  • poll events inside the loop pygame.event.get()

Example:

import pygame
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)

clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
    pygame.event.poll()
    clock.tick(10)
pmoleri
  • 4,238
  • 1
  • 15
  • 25
1
import winsound
winsound.PlaySound(filename [as string with path if necessary], flag [integer z.B. 1 for asynchronous] )

This works fine with Windows 8 and Python 2.7.6 and *.wav files

The different flags you can check if you type winsound. and look for the autofill list.
The flags are like SND_FILENAME or similar.
If you type them into the editor like: *winsound.SND_FILENAME you get the integer to apply in the PLaySound command from above

Have fun

EBH
  • 10,350
  • 3
  • 34
  • 59
DrWienands
  • 11
  • 2
1

Pygame sound not working fix

INFO

Try reinstalling pygame or upgrading it I had version 1.9.1 which I upgraded to 1.9.3 by the following command

pip install pygame --upgrade

Note:


If you install it with easy_install it doesn't work . Not sure but when I tried with easy_install it doesn't work


The code is the same as yours but i just replaced it with my music file to check it.
You can see it in the image :

In this image

.wav files seem to work well

I checked it with .mp3 files and found that it does'nt work. You can convert .mp3 to .wav from : Converter

Or use PyMedia
If you do not want to convert it using any website.
You can also convert it with pydub.

You can download pydub from this

link

Or type this command in the command line:

pip install pydub
Anonymous
  • 596
  • 1
  • 9
  • 26
0

Usually, Pygame will not play mp3 files. You could test to see if .wav and .ogg files will play first, to make sure your code is correct (based on what you pasted, it seems to be right). I suggest converting your mp3 sounds to ogg for Pygame.

Harshit Bansal
  • 107
  • 1
  • 3
0

You have to add this to the end, in order for pygame to be happy:

while True:
    for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()

Also, import sys.

-2

it would be easier if u changed the mp3 to ogg format ,try this.. its simple

from pygame import *
pygame.init()
pygame.mixer.music.load("only one.ogg")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.3)

vote the answer if its working, if not tell me whats wrong

user1474157
  • 1,379
  • 2
  • 11
  • 13
  • 1. Use `import pygame` before `pygame.init()`. If you `from pygame import *`, pygame won't be defined. And you get other issues. 2. `pygame.mixer.music.load` gives `pygame.error: Unrecognized file format`. – Charles Merriam Jan 29 '15 at 01:01