I have sounds working, in my game, pretty well. Except, if the sound is rapidly played, or multiple sounds overlap, only one of the sounds play and the rest are cut off. Is there a way to fix this? I'm using:
pygame.mixer.Sound.play()
to play sounds.
This is probably because you have the:
pygame.mixer.music.play()
Inside a loop. Try:
pygame.mixer.music.play()
pygame.time.wait()#Inside the () of pygame.time.wait(), type the length of the audio in seconds,
That should do it!
Related
I am trying to develop a basic experiment in the PsychoPy Builder where a silent movie plays in the background while I go through a .csv list to play a sequential series of about 100 very short sounds (about 300 milliseconds each). When I play the .csv list by itself, the sounds play perfectly like I would expect, but when I add the video component, only the first sound in the list will play and it won’t play the entire list of sounds. The video does continue to play, but somehow seems to interfere with the sound component or reading through the .csv file.
I tried deactivating the audio of the silent movie because I thought this could interfere with the sound component, but this does not resolve the issue.
It seems the program is waiting for the video to end before it can repeat the loop and go to the next sound in the list, but instead the program just closes as soon as the video ends. Furthermore, I want the single video to be playing while the sound component loops through the list, but I don't know how to apply the loop only to one component and not the video component that is supposed to play simultaneously with the sounds.
I feel like this should be very easy to achieve, yet I could not find someone with a similar issue when googling a solution. Your help is much appreciated.
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.
Recently I have been trying to make a game in python with pygame and have so far succeeded. My endeavors came to a sudden halt when I tested the game after implementing sound code, and found that no sound played. I have looked around the forums and most people say to convert the .mp3 to a .wav or .ogg, and convert I did, but to no avail. I was thinking if it isn't the file, then maybe there must be something off with my code. Can you guys tell me what I can do to fix it?
The code is as follows
pygame.mixer.init()
pygame.mixer.music.load('BennyHill.wav')
pygame.mixer.music.play(-1, 0.0)
I am just a beginner at the moment, so it might look completely wrong to you experts out there
Do pygame.music.play(-1). In my programs that always works.
The docs are sometimes incorrect with needed/optional arguments.
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
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.