Python: Playing a music in the background? - python

I am currently making a game in Python. I am not using PyGame, just the console (non-GUI)
When you start the game, you will get the logo to the game, and a lot of information about the "journey" you just started. There is a lot of text, so while the text is scrolling, I want to have a song played in the background.
I start the music with the following code:
def new_game():
import winsound
winsound.PlaySound("intro.wav", winsound.SND_ALIAS)
LVL1_INTRO()
The only problem is: it won't continue to LVL1_INTRO() until the music have stopped playing. It's a problem, as the music is approximately 1-2 minutes long.
Is there any way to fix this? After the music have started, it will continue with LVL1_INTRO()
If it is possible, I would be happy if there is a code for stopping the music as well, so I don't need to start cutting the music, and make it exactly the same lenght as the intro.
Thank you very much!

According to the documentation you use the SND_ASYNC flag.
winsound.SND_ASYNC
Return immediately, allowing sounds to play asynchronously.
To stop playing, call PlaySound with a NONE argument.
winsound.PlaySound(None, winsound.SND_ASYNC)

I don't have experience with this module, but it looks as though you can play sounds asynchronously. See http://docs.python.org/2/library/winsound.html and look at SND_ASYNC.

Related

Winsound Async not playing sound

i know this question has been answered, but the answer didn't work for me. Python winsound, ASYNC flag not working?
I'm trying to play a sound file using winsound asynchronously, but if I use
winsound.PlaySound("sound.wav",winsound.SND_FILENAME|winsound.SND_ASYNC)
It won't play the sound, but if I use
winsound.PlaySound("sound.wav",winsound.SND_FILENAME)
It will, but i need it to be asynchronously so i can stop the audio if needed.
Is there something i'm doing wrong here?

Python-Pyglet- Sound player stops after ~1-minute

Hey so my music stops playing after ~1 minute. The sound file is ~15 minutes long, so it shouldn't be stopping. It also makes other sounds in the program not work. I am also trying to set the player to loop, so if someone knows how to do that please check my code. Any help appreciated!
import pyglet
music = pyglet.media.load('warmusic.wav', streaming= False)
player = pyglet.media.Player()
player.queue(music)
player.volume=.5
player.eos_action='loop'
player.play()
pyglet.app.run()

Python Random Audio

I have no experience working with audio, and I am looking for some direction on where I could find music that when a button is clicked in python it will play a random song from a selection of some. I have already written a program for my aunt, and I am trying to add audio to the program. She is a devout catholic, and I would like to use catholic songs. I have spent the past hour searching for information on python audio but have not found anything of use. Any help is greatly appreciated.
Probably the easiest way to play music is to use Pygame. It has two modules that are related to playing music which are pygame.mixer and pygame.music. Here is the link to the Pygame music module. Also you can look at This thread where people discuss their recommendations for Python and music.

Python - winsounds, how to play 2 sounds at the same time

I wrote this code, where I make 3 buttons and then when I hit 1 button, it plays sound A, I hit button 2, it plays sound B, etc.
from winsound import *
PlaySound("ooooOOooo.wav", SND_ASYNC)
PlaySound("WOOOWoooowooo", SND_ASYNC)
The buttons work and the sounds come out fine, but when I click 1 button and then second button right after that, it stops the first sound and plays the second.
How to make them play at the same time, so that the first still plays while the second one is added to play them togeter. I thought the mistake might be in flags, I tried a few combinations with the "|" operator but the problem still remains :(
Here are the resources:
http://docs.python.org/2/library/winsound.html
I would prefer sticking to the python std library please :)
Thank you!
According to the documentation
You need to do the following when using filenames:
PlaySound("ooooOOooo.wav", SND_ASYNC | SND_FILENAME)
PlaySound("WOOOWoooowooo.wav", SND_ASYNC | SND_FILENAME)
failing to do so is probably causing a None sound call to PlaySound which will cause:
If the sound parameter is None, any currently playing waveform sound is stopped. If the system indicates an error, RuntimeError is raised.
Well, I thought using other modules is bad, but pygame works fine. Sounds are played well and importing this module into the cx_freezer is easy :)

Python/Tkinter Audio Player

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.

Categories

Resources