What's a cross platform way to play a sound file in python?
I tried playing a .wav file using pyaudio. It works great on windows, but doesn't work in Ubuntu when another device is using sound.
The error is "IOError: [Errorno Invalid output device (no default output device)] -9996
Is there another library I could try to use? Another method?
Asked by: Anna213 | Posted: 28-01-2022
Answer 1
You can use wxPython
sound = wx.Sound('sound.wav')
sound.Play(wx.SOUND_SYNC)
or
sound.Play(wx.SOUND_ASYNC)
Here is an example from the wxPython demo.
Answered by: Kellan318 | Posted: 01-03-2022Answer 2
Have you looked at pymedia? It looks as easy as this to play a WAV file:
import time, wave, pymedia.audio.sound as sound
f= wave.open('YOUR FILE NAME', 'rb')
sampleRate= f.getframerate()
channels= f.getnchannels()
format= sound.AFMT_S16_LE
snd= sound.Output(sampleRate, channels, format)
s= f.readframes(300000)
snd.play(s)
while snd.isPlaying(): time.sleep(0.05)
Ref: http://pymedia.org/tut/play_wav.html
Of course, you can have a look at the Python wiki under Audio (http://wiki.python.org/moin/Audio/) for other libraries such as https://docs.python.org/library/wave.html or again in Python's wiki under Game Libraries (http://wiki.python.org/moin/PythonGameLibraries) that will point you to bindings to OpenAL or Pygame that has sound modules.
And finally, although I don't know the limitations of pyaudio, your error message sounds more like the library is not able to find the default output device more than the device is in use by another process. Maybe have a look at what output device is returned by the get_default_output_device_info
of pyaudio and compare it to whatever's your default setting in Ubuntu.
Answer 3
You can try Simpleaudio:
> pip install simpleaudio
Then:
import simpleaudio as sa
wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
play_obj = wave_obj.play()
play_obj.wait_done()
Answered by: Agata866 | Posted: 01-03-2022
Answer 4
I'm not absolutely sure if that fulfills your requirements, but I immediately thought PyGame
http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound
from pygame import mixer
mixer.init()
s = mixer.Sound('sound.wav')
s.play()
Answered by: Fiona276 | Posted: 01-03-2022
Answer 5
I found playsound
to be the simplest.
from playsound import playsound
is_synchronus = False
playsound(r"C:\Windows\Media\chimes.wav", is_synchronus)
Answered by: Dexter917 | Posted: 01-03-2022
Similar questions
Cross Platform SWF Playback with Python?
I'm looking for different solutions to playing back SWF files on Windows, OSX and Linux using Python. Ideally I'd like to embed the player inside a wxPython frame/window.
One possibility I'm investigating is the Mozilla XPCOM framework since its used by FireFox to load the Flash plugin within the browser.
platform - How do I check if I'm running on Windows in Python?
This question already has answers here:
macos - New to Mac Platform, Trying to get back to default python install
I've been trying to follow this blog post to get my python version back to the snow leopard default. I have followed the first two steps without a problem but am lost when it comes to 3 and 4. I installed Python 2.6.4 but I assume the instructions are pretty similar. Thanks for your help!...
file - Cross platform way to list disk drives on Linux, Windows and Mac using Python?
I am using Python2.6. I am trying to list the disk drives that a system may have.
On Windows, it may be something like C:/, D:/, E:/, etc. On Linux, it may be something like /boot, /media/SDCard, etc. And I don't know what it's like on a Mac. Maybe something under /Volumes.
Does anyone know of a cross platform way (that is, one which works on...
python - SWIG cross platform
My application is using SWIG to communicate between c++ and python on windows.
suppose if my interface is "example.h"
swig is generating example.py, example_wrap.cxx
/* File : example.i */
%module example
%{
#include "example.h"
%}
%include "std_string.i"
%include "std_wstring.i"
%include "example.h"
I am porting my application to MAC. Do i need to generate example.py, ...
What do i build my alpha platform on, PHP, Ruby, Python, .NET, etc?
I am developing a social networking website in the facebook/foursquare-ish space. I have gotten such varied feedback on what platform I should develop in. Of course it will be heavily influenced by who I hire, but i was hoping for a little additional feedback from the larger community. Thanks.
Finding usb gps in cross platform python
I'm working on a python app that's reading from a gps usb dongle. This far everything has been running in ubuntu/debian based systems where I communicated with the gps in a rather blunt way of scanning all of /dev/ttyUSB0-9 with pySerial for something speaking NMEA sentences on 38400 baud. Now I have been asked to get this app working cross platform and I'm a bit confused on which would be the best way of finding the gps d...
python - How to design a website on Django platform?
Apologies if my question is misleading. Here's what I mean. In Visual Studio 2010 for example, you could visually design a website or a web application as well as add C# code here and there. How would I design my website without an IDE, just by using Python IDLE and Django platform.
Would I need to use something like Dreamweaver to design the front-end part and then link it with Django? And if that is so, how diff...
Running Python Code on Xampp on windows platform
XAMPP-PYTHON-WINDOWS
I have installed Xampp. I'm running the apache web server and mySql service. I want to host my python code on my web server. However, I am having a hard time setting up python with Xampp. I read about modwsgi, downloaded it and pasted it in the modules folder. I have python 3.2 installed on my C drive.
Please let me know what should I do next, as in where should i paste...
os.walk - python listing dirs in a different order based upon platform
I am writing and testing code on XPsp3 w/ python 2.7. I am running the code on 2003 server w/ python 2.7. My dir structure will look something like this
d:\ssptemp
d:\ssptemp\ssp9-1
d:\ssptemp\ssp9-2
d:\ssptemp\ssp9-3
d:\ssptemp\ssp9-4
d:\ssptemp\ssp10-1
d:\ssptemp\ssp10-2
d:\ssptemp\ssp10-3
d:\ssptemp\ssp10-4
Inside each directory there is one or more files that will have "IWPCPatch...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python