Pygame - Play sounds simultaneously - python

I am making a Game using Pygame and what I'm trying to do is to have a main sound for every level and some default sounds being heard when collecting points (e.g.)
So, I load the main level music using:
pygame.mixer.music.load(music_file)
pygame.mixer.music.play(-1)
Now, what I want to do is to play a specific sound whenever a player collects a point. I cannot stop the music using:
pygame.mixer.music.stop()
pygame.mixer.music.load(point_music)
pygame.mixer.music.play()
because the level's music will stop playing.
So, I've tried doing something like this:
points_sound = pygame.mixer.Sound("point.mp3")
points_sound.play()
I know that sound playing in pygame runs on its own thread but I am sure that the program/game does not terminate prior to finish playing the sound.
Long story short: The player can collect points but I am unable to make pygame play the collecting points sound.

As sr2222 said in comments:
Docs say mp3 support is limited for music and that only OGG and WAV
are supported for Sound. Have you tried one of the formats that are
officially supported?
Try out OGG or WAV formats for your sounds instead, that should work.

points_sound.play() should return a channel object. This object is necessary for playing the sound.
points_channel = points_sound.play()
This has me helped in my own case.

Related

pygame mixer music change playback speed

I want to playback an .mp3 file at 50% it's original speed, naturally pitched down (i.e. keeping the pitch is not my requirement).
I am using pygame.mixer and found several solutions (such as this one) that suggest changing the frequency on initialization should do the job as shown below, however that does not work for me.
from pygame import mixer
mixer.init(frequency=22050) # my track.mp3 has a 44100 sample rate
mixer.music.load("track.mp3")
mixer.music.play()
Am I doing something wrong? No matter to what value i change frequency it does not at all change the playback speed or pitch of the music played.
I found this solution here, with which I am able to speed down my track and play it back using mixer.Sound(speed_change(sound, 0.5).raw_data()).play(), however this method does not allow me to use the mixer.music functionality such as seek(time) or setting the start time when using .play().
I am generally also open to solutions that are not using pygame mixer, however I do need to be able to stop and/or pause the playback and have a seek functionality.
One way to do this would be to use this Audio Slowdown Software.]1
This would have the advantage of being easily reusable without having to type extra code or extra lines.

pydub.playback.play() not playing sound

I am trying to play a .wav file in Python like so:
from pydub import AudioSegment
from pydub.playback import play
sound = AudioSegment.from_wav('test.wav')
play(sound)
But the sound is not playing. When I type just sound I get a box with the option to play, as expected (see picture below). When I play the sound from this box, it sounds like it should. But I want to play the sound automatically using the play function.
Does anyone know why play is not working here, or know of a workaround?
In order to play audio with pydub you need to install one of these:
You can read more about it here: https://github.com/jiaaro/pydub#playback

Pygame Audio Stops When Window Switches?

I wrote a music player in Python using Pygame and Pydub(I used Pygame for actually playing the music, while Pydub is probably unrelated to the issue).
The music works fine even when windows are switched unless I switch to another Pygame window. I thought this effect would go away if I compiled it(cx_freeze), but that didn't work.
So I was wondering if there is any way to let the music keep playing when the window is switched to another Pygame window.
I used pygame.mixer.music instead of Sound objects if that might somehow be related.
Thanks in advance!
Not exactly a fix, but it seems that if I disable the mixer(pygame.mixer.quit()) it doesn't cancel out the audio. Unfortunately, I haven't found a way to use audio in two pygame windows at once.
Feel free to post another answer if anyone figures out a full fix.

Pygame mixer clicking sound

Okay here's my code:
import pygame
pygame.init()
pygame.mixer.init()
track1 = pygame.mixer.Sound("boink.ogg")
track1.play()
So I am using a mac and I used homebrew to download the pygame 64 bit version. Everything works well, but when I try to make sounds using the mixer, all that I hear is a clicking sound. Has anyone experienced this in the past that may be able to help?
Also I have tried this with many different ogg files, so it is not something wrong with the sound file.
Can you try this?
import pygame
import time
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
song = pygame.mixer.Sound('boink.ogg')
song.play()
time.sleep(song.get_length())
Not to sound crude, but have you tried playing a file without using any kind of object? , just play simple sound file and see if it works? I usually start that way.
Here is what I tried as basics (and it works). Do mind, this is on windows as I do not own a MAC, but it will provide insight into if at all file is playing.
Also, try playing different format files (wav , mp3 etc).
import pygame
pygame.mixer.init()
pygame.mixer.music.load('boink.ogg')
pygame.mixer.music.play(0)
I had the same click when playing an mp3 with the pygame.mixer.Sound and .play code.
This worked for me:
pygame.mixer.pre_init()
#then instantiate and start your controller
pygame.mixer.init()
#then in your button click or wherever
pygame.mixer.music.load('mysound.mp3')
pygame.mixer.music.play()
A few things you can try:
Make sure that the path to your sound file is correct.
Turns out Pygame's pygame.mixer.Sound() only accepts Mono WAV sound files.
for my second point, you can go here to convert your ogg file to mono wav files.

Python - Pygame - Get if specific sound is playing

Pygame's mixer module has pygame.mixer.get_busy which returns a simple boolean.
My problem is that I have sound playing constantly, like explosions and gunshots, and need to know when a specific sound is playing to prevent game dialogue from overlapping.
I considered making a list of currently playing dialogue, creating a timer that counts down as each sound is triggered,but that would require me to add an sound Effects (my module that handles sounds) update in the main game loop.
This seems messy, and like a giant slowdown.
Is there a cleaner way to do this?
You can use the channel object.
Play a specific type of audio in a channel and check to see if the sound is playing.
import pygame
def main(filepath):
pygame.mixer.init()
# If you want more channels, change 8 to a desired number. 8 is the default number of channel
pygame.mixer.set_num_channels(8)
# This is the sound channel
voice = pygame.mixer.Channel(5)
sound = pygame.mixer.Sound(filepath)
voice.play(sound)
if voice.get_busy():
#Do Something

Categories