This question already has answers here:
how to play wav file in python?
(5 answers)
How can I play an mp3 with pygame?
(7 answers)
Closed 1 year ago.
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?
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)
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
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
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.
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.
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
Related
I am trying to learn how to make a game of Snake in Python. I found a tutorial using pygame, but I'm having trouble making it work. Here's my code :
import os, pygame
os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()
dis = pygame.display.set_mode((400,300))
pygame.display.set_caption('Test')
while 1 :
pygame.display.update()
pygame.quit()
quit()
The problem is that the game window simply doesn't open. I get ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred errors but I think those are related to the sound. Other than that, no message in the console.
The code is running in a debian 11 VM inside a Windows 7 host.
Delete the os.environ["SDL_VIDEODRIVER"] = "dummy"
From this
You can use PyGame without opening a visible display (e.g. for testing, or for integrating with other frameworks that have their own displays) by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.
As the documentation states, the dummy mode is designed to create an SDL environment without opening a window. Because in this case, we do actually want to create a window, You should remove the line os.environ["SDL_VIDEODRIVER"] = "dummy". This does also mean that you don't have to import the os module.
I am trying to play a file, but I do not know the command. Here is the image as to what my project looks like, and I am wondering if there is a way I can play the audio.
My suggestion is pygame. Install via:
pip3 install pygame
Download a .wav file to your .py file direction. Define a push button and connect to this function:
def somesound(self):
pygame.mixer.init()
pygame.mixer.music.load('somesound.wav') # Pygame allows *.wav
pygame.mixer.music.play(1) # For 1 second
I seem to be having difficulty with executing a pygame program on boot up. I have a raspberry pi 3 model B running the latest versions of the required libraries. I am simply trying to play a wav file through pygame when a button is pressed. I substituted an “led.on” script for the pygame to ensure my program was being started on boot-up and it ran flawlessly. However, when I try to use pygame on boot up it will not play the audio file. Although pygame will play the file in the terminal, it will not play on startup.
I am using crontab to run the program from boot. Using the following line:
#reboot python /home/pi/test.wav &
And the python code:
from gpiozero import Button
import pygame.mixer
pygame.mixer.init()
song=pygame.mixer.music.load(‘/home/pi/test.wav’)
btn=Button(27)
while True:
btn.when_pressed=(pygame.mixer.music.play)
Any help would be greatly appreciated, thank you!
This should solve your question
pygame.mixer.music.play()
play should be called with () as it's a fn
use
pygame.mixer.music.play(-1)
for looping the music
I have been trying to run sound in background while App is running. I am Arch Linux and Python 3.8
I have tried playsound as like this
playsound('music.mp3', False)
but I get error saying system not supported. I have also tried pygame following way:
from pygame import mixer
mixer.init()
mixer.music.load("music.mp3")
mixer.music.play()
But I get error pygame.error: Unrecognized audio format. Is there any other way I can run music in background some task is executing on GUI with tkinter. The program will run on arch and Ubuntu.
Try this code it will run the background music continuously till the app is running
mixer.music.play(-1)
Try this:
mixer.music.play(-1)
And if you want your background music to be stopped in 10s then add this too:
gui_name.after(10000, mixer.music.stop)
Hope it will help you.
so i wanted to make a game...and pygame doesn't seem to be working for me. I get this error, "
AttributeError: module 'pygame' has no attribute 'init'"
and ive tried every other forum and cant find help...my game is called roll and I'm using the correct pygame. What do i do?
import pygame
pygame.init()
gamedisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('roll.io')
clock = pygame.time.Clock()
dead = False
while not dead:
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead = True
print(event)
pygame.display.updat()
clock.tick(30)
pygame.quit()
quit()
I've also had some trouble installing pygame for the latest python version, mostly because all guides are either old or don't support the latest python version.
So this is what worked for me:
First download(I uploaded it for you because I can't even find the place I originally downloaded it from...) it from here then copy the file and paste it into [your python folder]/scripts, then open cmd type cd [location of your script folder] and finally type pip3 install pygame-1.9.2a0-cp35-none-win32.whl and don't try to rename it to a shorter file name because for some reason it won't work.
Have you tried including
from pygame.locals import *