Can't play .mp3 file using winsound on windows - python

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

Related

python winsound.playsound unable to find my sound file

I've been trying to add some music to the game I've been making. I've been trying to use winsound because it allows you to stop a sound mid-way through playing. The problem is that winsound seems unable to locate my sound file.
I've tried using different modules such as the playsound module, which is able to play my music just fine, but winsound can't for some reason. Unfortunately I can't just use the playsound module for my game because it doesn't provide a way to stop sounds midway through playing.
Here's what I've tried:
#testing if winsound functions
import winsound
import playsound
#The file I want to play, test1.wav, is saved in the same folder as this file.
#Playing the sound using playsound, using a local directory
playsound.playsound('test1.wav')
#this works and plays the sound as intended
#Playing the sound using playsound, using a global directory
playsound.playsound(r'C:\Users\61490\Documents\Python\pano tiles\test1.wav')
#this also works and plays the sound as intended
#Playing the sound using winsound, using a local directory
winsound.PlaySound('test1.wav', winsound.SND_FILENAME)
#This only plays the windows default error sound
#Playing the sound using winsound, using a global directory
winsound.PlaySound(r'C:\Users\61490\Documents\Python\pano tiles\test1.wav', winsound.SND_FILENAME)
#This also only plays the windows default error sound
Does anyone know why this might be?
For me, it works with the winsound.SND_FILENAME just like you tried, but I know that you can replace the SND_FILENAME tag with SND_ALIAS and it should work, but since the other way doesn't work for you it is possible that it still won't work.
So it should give you this:
import winsound
# From a local directory
winsound.PlaySound('test1.wav', winsound.SND_ALIAS)
# Form a global directory
winsound.PlaySound(
r'C:\Users\61490\Documents\Python\pano tiles\test1.wav',
winsound.SND_ALIAS
)
So if it doesn't make sure that you've installed the library correctly or

pygame sound unable to open file

I am trying to make a simple program that plays some narration, and has a sound effect on a trigger. Being new to python, I am just building up functions in layers. Getting music to work was easy, as it should be, but I cannot get sound to work.
I have read of a few people with similar issues, but either they are also unsolved, or the symptoms are slightly different, and the solutions don't work for me.
I have tried this on a pi3 (which is the end target), and windows 7, both running latest python 3, and pygame.
import pygame
import time
import os
pygame.mixer.init()
#pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
pygame.mixer.music.load("English.mp3")
pygame.mixer.music.play()
print (os.getcwd())
shot = pygame.mixer.Sound("gun-gunshot-02.wav")
shot.play()
while True:
Time.sleep(1) # for testing and irritation prevention if sound ever plays
shot.play()
I should also note that the sound effect does work if I play it using music, but of course it replaces the narration.
Error is as follows same on both machines, same with both mp3 and wav:
C:\Users\me\Documents\Interrupter
Traceback (most recent call last):
File "C:/Users/me/Documents/Interrupter/simpletest.py", line 11, in
shot = pygame.mixer.Sound("gun-gunshot-02.wav")
pygame.error: Unable to open file 'gun-gunshot-02.wav'
Thanks in advance
So I have solved the issue, which is of file compatibility.
I don't know what the right kind of wav file is (other than uncompressed). I had tried using a wav but it did not work.
However I finally tried .ogg since that was the only thing listed as working in the online manual for pygame. As soon as I used .ogg everything worked as planned.
For anyone else with this issue I used Audacity to export my .mp3 and .wav files to .ogg using defaults.
Pygame is supporting only 2 type of files.
OGG and WAV
Sound can be loaded from an OGG audio file
or
from an uncompressed WAV.
for more information please visit to
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound

How do you play a sound file without opening any media players?

I'm writing a simple script that I would like to have notify me of an event with a sound file on my computer.
While I can play a sound file by doing something like:
import webbrowser
webbrowser.open("C:\Users\User\Desktop\Test\Test.mp3")
That will open up VLC or whatever media player I have, which could alt-tab me / interfere with whatever I was doing.
Is there a way to play the sound file without anything popping up? A perfect example of what I am looking to do would be using something like making a beep sound using winsound as such:
import winsound
Freq = 600 # Set Frequency To 600 Hertz
Dur = 800 # Set Duration To 800 ms == 0.8 second(s)
winsound.Beep(Freq,Dur)
Which makes a quick beeping sound without opening any new windows.
This does the trick.
Firstly, install pyglet:
pip install pyglet
Now download and install AVbin from here
Do check where AVbin is installed as now you have to go to the installation directory and copy the avbin.dll to the directory where you saved your code.
Finally,run this code:
import pyglet
pyglet.lib.load_library('avbin')
pyglet.have_avbin=True
song = pyglet.media.load('filename.mp3')#your file name
song.play()
pyglet.app.run()
Make sure your music file is in the same directory as your code .py file.
Did this from my experience and it worked.
Solved by #eryksun in the comments with the solution of:
winsound.PlaySound(wav_path, winsound.SND_FILENAME | winsound.SND_ASYNC)
This was the best solution for I've seen for what I was looking for. It uses default libraries, only takes up a single straight-forward line, and does what I need it to.
You should use pygame to solve it
def music(music):
pygame.mixer.init()
pygame.mixer.music.load(music)
pygame.mixer.music.play()
it will play sound without opening vlc media player
search google for more otherwise this will be enough to know

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.

Why doesn't my PyGame mixer play sounds,?

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')

Categories