im currently having trouble with the python 3rd party library pygame.
Other posts on stackoverflow didn't help me so i opened a new post.
I want to load a *.mp3 file into the program and play it. My code so far (copied from other posts):
import pygame,time,sys
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
print ("Mixer settings", pygame.mixer.get_init())
print ("Mixer channels", pygame.mixer.get_num_channels())
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.load("test.mp3")
pygame.mixer.music.play()
clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
clock.tick(30)
Here is a link to a screenshot from the Windows Sound panel: http://i.imgur.com/fUvJXof.png
I see that there is some output, but i cant hear anything on my headphones.
I hope some has an idea what the problem is.
Thanks
Check your hardware/audio drivers. It doesn't sound like (pardon the pun!) an issue with python or pygame. Does your audio work outside python? Also, if you've got other programs open, they can sometimes take control of your audio drivers and not allow other programs (in this case python) to output.
Related
I made an app(although not finish yet)with kivy module. It can let me upload my audio file in and play it.
It works like this if I press the play button on the right corner, it will play audio file (using vlc module for mp3 and kivy SoundLoader for other types of audio file)
This app is for iPad use(although I currently test it on pc), but I don't know how to make it background playing, I mean making it can even play sound while the screen is black or while using other app on the screen like wut youtube premieum does.
This is important for me because I tried to develope this app in order to listen to my music and do my homework with ipad page meantime.
Pls tell me some method if you have any idea, thank you!
well I haven't tried much methods bc I currently didn't fine out much information.
U actually will found people talking about pygame running other function while BGM is playing (which is not wut I want) if u just search "python background playing" on search engines
I've been working on this for a while but everything I find seems to be a dead end so I could use some help troubleshooting.
import pygame
file = open("C:\\Users\\MyName\\Music\\oggTest.ogg")
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
This code is supposed to grab an audio file and play it using the mixer from pygame. Very simple, and I've seen other questions here asking about how to do it but mine's still not working for some reason. Right now, I have no errors when I'm running the code. The only problem is that nothing happens. No audio gets played. I have a hunch that it has something to do with my IDE (pycharm), but I don't know for sure if that's the case. Also, the filepath part is a bit confusing to me. I don't see any reason why it wouldn't work but perhaps I'm missing something obvious. Thanks in advance for helping out.
As per the advice of a YT video, I converted the mp3 file I originally had to an ogg file. Not sure if that was necessary, and honestly I'd prefer not to have to do it in the future.
Okay, I found the answer after a bit more research. Taking out the pygame.init() worked. I guess it didn't like doing both pygame.init() and pygame.mixer.init() for some reason.
I can't hear the sounds I am trying to play with pygame. This is my code.
import pygame
pygame.init()
pygame.mixer.music.load(r"C:\Users\Isaiah\Desktop\easy_going.mp3")
pygame.mixer.music.play()
Any help would be great!
Thanks!
MP3 support is limited with Pygame; use .wav instead. Fortunately you can easily convert .mp3 files to .wav files with an online conversions tool.
You also need to add a delay loop at the end to keep your program from closing prematurely.
Convert your .mp3 to a .wav and then try running this modified code.
import pygame
#Replaced init() with mixer.init()
pygame.mixer.init()
sound = pygame.mixer.Sound(r"C:\Users\Isaiah\Desktop\easy_going.wav")
s = pygame.mixer.Sound.play(sound)
#Delay loop
while s.get_busy():
pygame.time.delay(100)
Good luck!
I've encountered an odd bug using psychopy recently, where my script does not kill psychopy when it is terminating.
This only happens when I add a sound to my routine and when I make psychopy generate a sound or play a sound from a file. In other words, just loading sound from psychopy does not result in my script being unable to quit. Only when I make a call to a sound as in s=sound.Sound(value="C", secs=0.15), and even if that sound ins't actually played in my routine by s.play()
I've tried googling to see if other people reported this issue but it seems I'm kinda lonely. I've tested it on two Macs specs below:
MacBook Pro runs:
OSX Yosemite
enthought python 2.7
latest psychopy (recently installed with easy_install)
pyglet 1.2
pyo 0.7.5
Intel Iris graphics card/built in
iMac runs:
everything similar to above
NVIDIA separate graphics card (don't remember model right now)
Here is some example code I am running. Does this quit fine for you?
Does anyone have suggestions regarding what might be going on?
Let me know if more info is needed about my system etc.
#Display a text together with a sound synced
#to the onset of the visual display
#---------------------------------------------
from psychopy import core, visual,sound
win = visual.Window([800,800], color='gray', allowGUI=False, winType='pyglet', monitor='testMonitor',units='pix')
stim = visual.TextStim(win, text="Hi!")
s=sound.Sound(value="C", secs=0.15)
startText=visual.TextStim(win, text="starting")
startText.draw()
win.flip()
core.wait(1)
for i in xrange(2):
for frameN in range(70):
stim.draw()
if frameN == 0:
win.flip()
s.play()
else:
win.flip()
win.flip()
core.wait(.2)
Thanks :)
I'm developing a media player. Right now it's a simple window with a button to load .wav files. The problem is I would like to implement a pause button now. But, when playing a audio file the GUI isn't accessible again (no buttons can be pushed) till the file is done playing. How can I make the GUI dynamic while an audio file is playing?
I'm using PyAudio, and their implementation doesn't allow this.
Probably you have to use threads for that. You have to play your audio file in a different thread than the gui mainloop so that the GUI keeps responding user input.
IMHO, wxpython is not so complicated and has some utility functions that would help to do what you want. Check the wxpython demo, you have several examples there.
You can alternatively use pygame mixer for the purpose , I made the same in pyqt and I did'nt require to implement threading . You can get the documentation of pygame mixer at https://www.pygame.org/docs/ref/mixer.html
Happy Coding .
Try this out:
Check the code https://drive.google.com/file/d/0B7ccI33Aew5fNVhwZ2puYTBuUFU/view?usp=sharing
I have used pygame also.Hope this helps.