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
Related
I am trying to play an audio file but it only makes the windows alert sound when I open/run the code. How do I fix this?
EDIT: nothing on winsound works it just makes the windows alert noise (windows background.wav) right click on the sound button (bottom right), click sounds, click on the first one and press test. that's the noise.
code:
import winsound
winsound.PlaySound('music.mp3', winsound.SND_ASYNC)
The winsound.PlaySound() function is just a thin wrapper around the PlaySound() Win32 API, which only plays waveform audios (WAV) and cannot recognize MP3 files.
If you're not willing to convert the file to WAV manually, you may use the MCI components.
The third-party playsound library provides a concise example for its usage, you can either install it or learn from its source code.
by using pygame I found:
from pygame import mixer # Load the required library
mixer.init()
mixer.music.load('music.mp3')
mixer.music.play()
which works fine
I'm using winsound in Python to play a short Om chanting sound at the end of my program (in order to emphasize how cool the plot generated by the program is). The Om sound I'm playing is stored in a .wav file. I'd like to make the sound trail off near the end of the clip, so that the volume smoothly reduces to zero and the clip does not end abruptly. How can I make that happen?
Here's the code I'm using to play a sound:
import winsound
winsound.PlaySound('om1.wav', winsound.SND_FILENAME)
Your best bet may be to edit the wav file. Add a fade out with audacity (open source), then just use your existing code to play it
winsound does't seem to provide fade-out. You may edit the .wav file using e.g. Audacity.
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.
My PyGame mixer in 2.7 won't work with the sound option. I can make it work with mixer.music but not with mixer.sound, with mixer.sound it makes a small ticking noise and then stops. Code:
import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
song = pygame.mixer.Sound("song.mp3")
pygame.mixer.Sound.play(song)
No error, it just won't play and gives a small ticking noise.
On windows 7-x64 btw.
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 just created an object called song.
instead of "pygame.mixer.Sound.play(song)" try this:
song.play()
This can easily be solved because your song file should be loaded as music, not as a normal sound. Therefore, the following code makes it work perfectly:
import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("song.mp3")
pygame.mixer.music.play()
Pygame does play mp3 files. I had the same problem but I found out the solution:
if you saved your mp3 file as 'filename.mp3', and you wrote down the .mp3 file extension yourself, then the filename in pygame's pygame.mixer.music.load() function must be written as 'filename.mp3.mp3', because python expects you to add the .mp3. Sometimes the .mp3 is already included in the filename if you manually saved it as that.
Therefore, try this:
pygame.mixer.music.load('filename.mp3.mp3')
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.