Python Raspberry PI PyGame Audio error - just a click [duplicate] - python

I'm quite new with raspberry pi and am very new but I am trying to play an audio file through a python file on a pi B. I played the audio from the command line using omxplayer and it worked fine but when i run the following code it plays only static.
from pygame import mixer
mixer.init()
wow = mixer.Sound('Wow.mp3')
wow.play()
time.sleep(5)
I tried using mixer.load('Wow.mp3') and other variations but still only get static. I tried putting things in the init like mixer.init(4800, -16, 1, 1024) and using pre_init but to no avail. I also tried adding disable_audio_dither=1 to config.txt. Any and all help would be greatly appreciated thank you very much.

Sometimes if sound is supposed to be playing but isn't, and the speakers are turned all the way up, it will play static instead. This is probably a computer problem, not a pygame problem. Check whether audio plays in other locations. If it does not, it is definitely a computer problem.
If using Linux (you probably are, Raspbian is a variant of Debian which is Linux), and you have identified that it is a computer problem, try initializing ALSA by typing alsactl init as root in the Linux Terminal. If this does not work and the above is true, then you are having some audio problems that will take a bit of research to fix.
If audio does play in other locations, then it is probably a file format issue. It's possible that the bitrate is different, the format is wrong, etc. Also, here is a quote pulled directly from Pygame documentation:
Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.
https://www.pygame.org/docs/ref/music.html
Other than this, there isn't much we can do to help you. A problem like this that isn't fixed by the above is usually a problem specific to very few computer setups. You will probably have to do some personal research.
If you need any more help, please notify me in the comments.

Related

find out which programs play sound python Python

I want to find out which programs are playing sound with a python code, I researched it a lot, even though I tried libraries like winsdk pycaw, I couldn't get any results
The simplest way is to use standard winsound module. Please, refer to documentation
If you are now under Windows, there are:
the bell character on the terminal
AppKit to play MacOS system sounds
pygame to play custom sound files
simpleaudio to play custom sound files
the beepy package

Raspberry pi with pygame.mixer audio produces only static

I'm quite new with raspberry pi and am very new but I am trying to play an audio file through a python file on a pi B. I played the audio from the command line using omxplayer and it worked fine but when i run the following code it plays only static.
from pygame import mixer
mixer.init()
wow = mixer.Sound('Wow.mp3')
wow.play()
time.sleep(5)
I tried using mixer.load('Wow.mp3') and other variations but still only get static. I tried putting things in the init like mixer.init(4800, -16, 1, 1024) and using pre_init but to no avail. I also tried adding disable_audio_dither=1 to config.txt. Any and all help would be greatly appreciated thank you very much.
Sometimes if sound is supposed to be playing but isn't, and the speakers are turned all the way up, it will play static instead. This is probably a computer problem, not a pygame problem. Check whether audio plays in other locations. If it does not, it is definitely a computer problem.
If using Linux (you probably are, Raspbian is a variant of Debian which is Linux), and you have identified that it is a computer problem, try initializing ALSA by typing alsactl init as root in the Linux Terminal. If this does not work and the above is true, then you are having some audio problems that will take a bit of research to fix.
If audio does play in other locations, then it is probably a file format issue. It's possible that the bitrate is different, the format is wrong, etc. Also, here is a quote pulled directly from Pygame documentation:
Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.
https://www.pygame.org/docs/ref/music.html
Other than this, there isn't much we can do to help you. A problem like this that isn't fixed by the above is usually a problem specific to very few computer setups. You will probably have to do some personal research.
If you need any more help, please notify me in the comments.

Python audio without root privileges?

I working in python on computers in a lab where I do not have root privileges and can't get any libraries installed. Someone I am helping is writing a game and wants to add audio. We can play sounds with something like this
subprocess.call(["afplay","gun_battle_sound-ReamProductions-1158375208.wav"])
but everything stops until the sound is done playing. Is there a way to keep things moving while the audio is playing? Is there a library that we can just copy? We are using the graphics module from Zelle, so I am looking for something like that - simple to use and install.
Thank you!
Pyglet
is completely copyable and easy to download.
Otherwise you will probably need to create a thread from threading just for the sound to play on.

Tablet Pressure with Python on Linux

Is there a simple way to get the pen pressure data from a usb tablet using python on Linux?
You can do it by reading input events on the input device node. I wrote some modules to do this. you can find it in the Pycopia project.
The disadvantage of this is that your program must run as root.
The powerdroid project also uses this, but that's old code now. You can see another example of synthesizing touch input in the devices module. It probably won't work anymore, but you might start with that.
Try using PySide, it's a QT Wrapper here: QTabletEvent.
Or you can use Python and PyGame: Here.

Playing and controlling mp3 files in Python?

First things first, I am a Python beginner, with a typical C++/Java background for object oriented stuff.
I was convinced to try Python for this current endeavor I am working on, and so far I like it. One issue I am having though is finding a good mp3 module.
I have tried TkSnack, which installed and ran fine with no errors(as long as my audio device wasn't busy) but it could never actually produce a sound, it just did nothing... I went online for help, and was disappointed with the amount of documentation.
So I decided to switch. I tried PyMad because it is in the standard repositories for Ubuntu as well. There was even less documentation on this, but I could make it play a sound. The only problem is that it requires a loop to constantly write/play the audio buffer. This makes it particularly hairy to handle playback control(in my opinion) cause I would have to run this in a separate thread or process, and somehow control the seek position for pause and such. This is a little too low level for why I am using Python. I liked the simplicity of TkSnack for its easy commands like "mysound.play()" or "mysound.pause()" rather than controlling a loop.
I also looked at pyMedia, which looks like it is the most up to date with documentation, but I can't get it to install on my machine. I get a "gcc exited with value 1" error or something like that when running the "python setup.py build" command.
So I am looking for any suggestions or help on one of these modules, or a completely different one, that is high level and easy to use for mp3s(and preferably other formats too) I am trying to have basic playback control(pause, stop, skip, seek) and I may also be streaming files too eventually(if I ever get there).
EDIT: I like the python bindings for Gstreamer, but is this a cross-platform solution?? I forgot to mention that as a requirement. But I always just associated GStreamer with Linux, would this work on other OSs?
EDIT: Wikipedia says yes.
Sorry I can't help you with PyMad or pyMedia, but I have other suggestions.
Existing music players written in Python:
Exaile
FUPlayer
Listen
All of the above use the Python bindings for the GStreamer multimedia framework. Docs for the bindings are scarce, but check here, here, here, and examples from the source distribution here.
I just had to deal with this, and from my research I think your best bets are pyglet and pygame. They're interface packages with built-in a/v support.

Categories