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

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 :)

Related

How do I make a function triggered by pressing right click?

I'm working on a game involving turtle for a school project and I want to end it when the player right clicks on anything but I've been researching for ages and I can't find how to key bind a right click and every module i import but turtle won't work.
I tried importing pyautogui, pydirectimput, tkinter and many more to try and find work arounds becuase it has to be a right click but the imports wont work. I've tried treating it like a normal key bind but that won't work; maybe I'm not using capitals and underscores correctly, I don't know how to do it and it's due in 7 hours. Please help.
I don't know turtle much, but I think this should work:
def bar():
# do stuff here
# Test this with either a 2 or a 3, idk which one it is
turtle.onscreenclick(bar, 2, True)
It will bind the right click event to this bar function.
Tell me if it does not work!
Edit: I had used the wrong method sorry!
It's actually "onscreenclick".

I want to have python hold down a key for me

I got my script to work with typing out keys, but for some reason, those keys do not work in Minecraft. Although, typing Minecraft messages in the chat are no problem. I'm trying to get python to move my own character.
In my script, I tried to use pyautogui, the mouse in Minecraft works but not keys such as w, a, s, or d. Is there another import or way to hold down a key that actually works in Minecraft?
def hold_W (hold_time):
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
Doing this and calling it worked for writing texts, but for some reason does not register in Minecraft? Any help?
If you used "Automate the Boring Stuff With Python" to learn this stuff, the book states that pyautogui cannot be used on video games on Windows. I am not sure how to fix this, but the link to the book is here: https://automatetheboringstuff.com/2e/chapter20/
Also, you can go to the documentation of pyautogui here: https://pyautogui.readthedocs.io/en/latest/ to see if the games feature has been added. My greatest regrets that I cannot solve this problem for you. If it makes you feel better, pyautogui does work on .io games.
Different video games have different ways of detecting keystrokes, so to find a thing that will work on all video games you might need to get your hands dirty and do some stuff I have no clue how to do.

(python) Different behaviour when called using threading.Timer

I want to make a little command line music player based on the python module "mp3play".
I want to check regularly, if a song has stopped playing (and eventually start a new song), but the user should be able to type new commands during that time (like pausing the music).
Therefor i tried to use threading.Timer for that. However, it gives me an error if i am inside the function that was called using the timer. the error does not occur when the function was called normally. Heres my (reduced) code:
from threading import Timer
global currentmusic
def rep():
b = currentmusic.isplaying() #this is where the error occurs
if b:
print "Music is playing"
else:
print "Music has stopped"
t=Timer(5.0,rep) #repeat every 5 seconds
t.start()
currentmusic=playrandomfile() #loads a song and starts playing it
rep() #call the first time
When rep() is called the second time, it gives me an MCI error in the function isplaying(), saying that it cannot read the device. My questions:
Am i making a mistake with the way the threading.Timer works? (and how can i fix it?)
Is there another way than threading.Timer to achieve the stuff i want?
My thoughts so far were, that it could be a problem to access currentmusic from another thread, but i am not sure. Also i dont know how to avoid it.
Thx for helping
Ive used mp3play in some projects, and it works fine for me.
IMO recursion threading is the problem.
Just remove the threading timer and leave the rep function call, it wont lag any pc.
You should use threading only for raw_input.

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.

Can't get winsound.Beep to work

I am trying to make some Beep noises with the winsound.Beep command. However when I am using winsound.Beep(500, 500) I do not hear anything. With winsound.MessageBeep and winsound.PlaySound however, I DO get to play sounds. Any ideas what I should do?
What I am trying to do: I want to write a little practicing program for training intervals: the computer sounds a first tone, then a second tone and you will have to guess what the tone interval is. For this I need pitched tones, tones for which I can set the frequency. I want to keep it as simple as possible, any pitched sound will do. I do not want to have to collect a set of .wav files or whatever. I want to make use of a tone generator which I think is available on most soundcards. winsound.Beep seems like something that can do this trick, but any other suggestions are welcome.
I had the exact same problem. winsound.Beep used to work just fine and then it suddenly stopped working (or that's what I thought). The problem was that someone (or some update) had turned off the System sounds, which prevented windows from playing the Beep sound, either manually or through my program. Try right clicking on the Speaker symbol, Open volume mixer and check whether System sounds is off or minimum volume.
I hope that helps!
Are you sure that your computer has a beep? A lot of recent computer remove the beep because it was annoying and most computer today have soundcard to play wav sound instead (the other sound that you are able to play).
You can also check to validate if it's activated on this page
IMO, I think that using the beep for other things than debugging is not a good idea.
EDIT
Mayby you can try this code to create a sound using a base wav with synth algorythm

Categories

Resources