Can't hear the sounds I am tring to play with pygame - python

I can't hear the sounds I am trying to play with pygame. This is my code.
import pygame
pygame.init()
pygame.mixer.music.load(r"C:\Users\Isaiah\Desktop\easy_going.mp3")
pygame.mixer.music.play()
Any help would be great!
Thanks!

MP3 support is limited with Pygame; use .wav instead. Fortunately you can easily convert .mp3 files to .wav files with an online conversions tool.
You also need to add a delay loop at the end to keep your program from closing prematurely.
Convert your .mp3 to a .wav and then try running this modified code.
import pygame
#Replaced init() with mixer.init()
pygame.mixer.init()
sound = pygame.mixer.Sound(r"C:\Users\Isaiah\Desktop\easy_going.wav")
s = pygame.mixer.Sound.play(sound)
#Delay loop
while s.get_busy():
pygame.time.delay(100)
Good luck!

Related

Is there a way to deliberately slow down audio being played through pygame? [duplicate]

quick question.
I'm running pygame under linux just to play some audio files.
I've got some .wav files and I'm having problems playing them back at the right speed.
import pygame.mixer, sys, time
#plays too fast
pygame.mixer.init(44100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
#plays too slow
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
I've ggogle code searched some stuff but everybody seems to be fine calling the init function with its default parameters. Can others try running this script and seeing if they get the same behavior or not? Does anybody know how to speed it up? Or adjust the speed for each file?
Thanks.
I had some mp3 audio tracks playing back slowed down. I updated the mixer frequency to be based on the mp3 sample rate using mutagen like so:
import pygame, mutagen.mp3
song_file = "your_music.mp3"
mp3 = mutagen.mp3.MP3(song_file)
pygame.mixer.init(frequency=mp3.info.sample_rate)
pygame.mixer.music.load(song_file)
pygame.mixer.music.play()
And it fixed the problem.
To improve Chris H answer. Here is a example of how to use the wave library.
import wave
import pygame
file_path = '/path/to/sound.wav'
file_wav = wave.open(file_path)
frequency = file_wav.getframerate()
pygame.mixer.init(frequency=frequency)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
Remember that if you want to change frequency or any other parameter used in pygame.mixer.init you must call pygame.mixer.quit first. Pygame documentation
Open your audio file in a free audio tool like Audacity. It will tell you the sampling rate of your media. It will also allow you to convert to a different sampling rate so all your sounds can be the same.
I figured it out...
There is a wave module http://docs.python.org/library/wave.html and it can read the sample rate for wav files.
If you're using Ogg Vorbis (.ogg) encoding, the same problem of stuttering audio happens. You'll have to read the frequency of what you're trying to play before initializing the mixer object.
Here's how to play .ogg audio with appropriate frequency using pygame.
from pyogg import VorbisFile
from pygame import mixer
# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()

Pygame Mixer not playing audio

I've been working on this for a while but everything I find seems to be a dead end so I could use some help troubleshooting.
import pygame
file = open("C:\\Users\\MyName\\Music\\oggTest.ogg")
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
This code is supposed to grab an audio file and play it using the mixer from pygame. Very simple, and I've seen other questions here asking about how to do it but mine's still not working for some reason. Right now, I have no errors when I'm running the code. The only problem is that nothing happens. No audio gets played. I have a hunch that it has something to do with my IDE (pycharm), but I don't know for sure if that's the case. Also, the filepath part is a bit confusing to me. I don't see any reason why it wouldn't work but perhaps I'm missing something obvious. Thanks in advance for helping out.
As per the advice of a YT video, I converted the mp3 file I originally had to an ogg file. Not sure if that was necessary, and honestly I'd prefer not to have to do it in the future.
Okay, I found the answer after a bit more research. Taking out the pygame.init() worked. I guess it didn't like doing both pygame.init() and pygame.mixer.init() for some reason.

pygame sound with Python 3.5 on Windows

im currently having trouble with the python 3rd party library pygame.
Other posts on stackoverflow didn't help me so i opened a new post.
I want to load a *.mp3 file into the program and play it. My code so far (copied from other posts):
import pygame,time,sys
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
print ("Mixer settings", pygame.mixer.get_init())
print ("Mixer channels", pygame.mixer.get_num_channels())
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.load("test.mp3")
pygame.mixer.music.play()
clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
clock.tick(30)
Here is a link to a screenshot from the Windows Sound panel: http://i.imgur.com/fUvJXof.png
I see that there is some output, but i cant hear anything on my headphones.
I hope some has an idea what the problem is.
Thanks
Check your hardware/audio drivers. It doesn't sound like (pardon the pun!) an issue with python or pygame. Does your audio work outside python? Also, if you've got other programs open, they can sometimes take control of your audio drivers and not allow other programs (in this case python) to output.

Python: Playing a music in the background?

I am currently making a game in Python. I am not using PyGame, just the console (non-GUI)
When you start the game, you will get the logo to the game, and a lot of information about the "journey" you just started. There is a lot of text, so while the text is scrolling, I want to have a song played in the background.
I start the music with the following code:
def new_game():
import winsound
winsound.PlaySound("intro.wav", winsound.SND_ALIAS)
LVL1_INTRO()
The only problem is: it won't continue to LVL1_INTRO() until the music have stopped playing. It's a problem, as the music is approximately 1-2 minutes long.
Is there any way to fix this? After the music have started, it will continue with LVL1_INTRO()
If it is possible, I would be happy if there is a code for stopping the music as well, so I don't need to start cutting the music, and make it exactly the same lenght as the intro.
Thank you very much!
According to the documentation you use the SND_ASYNC flag.
winsound.SND_ASYNC
Return immediately, allowing sounds to play asynchronously.
To stop playing, call PlaySound with a NONE argument.
winsound.PlaySound(None, winsound.SND_ASYNC)
I don't have experience with this module, but it looks as though you can play sounds asynchronously. See http://docs.python.org/2/library/winsound.html and look at SND_ASYNC.

How do I play a sound in Python?

I was wondering is there anyway I could play a sound without importing an external library like pygame. Something like this:
import os
import sound
mysound = sound.load("mysound.mp3") # gets the sound "mysound.mp3"
while True:
input("Press enter to play sound")
sound.play(mysound) # plays the sound mysound
os.system('cls') # clears the console
You cannot. You need some library.
The standard library has some functions for playing .wav files (see winsound.PlaySound on Windows and the slightly outdated ossaudiodev for Linux), but AFAIK no methods for loading .mp3 files.
This question has several answers listing various third-party modules for playing sound.

Categories

Resources