Winsound Async not playing sound - python

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?

Related

Play sound through Microphone Windows 10

I wanted to make a soundboard that works in voice calls. I tried a few methods, like the play sound module but they don't do what I want them to do. Would I need a lower level library to accomplish something like this and if so what should I be looking for?

Pygame Mixer not playing audio

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.

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: Playing a music in the background?

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.

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