I am using winsound to play a swoosh sound:
winsound.PlaySound("D:\GamesImade\pythonpong\bounce.wav", winsound.SND_ASYNC)
When I run it with my IDE it works. But if I run the exe file by itself it doesn't work, it plays the windows error sound.
I had a line of code that was supposed to run at a certain point and the code was accurately recognizing when the sound was supposed to be played, but it was only playing a windows error sound.
I changed the code from:
winsound.PlaySound("sound.wav", winsound.SND_ASYNC)
To:
winsound.PlaySound('C:/Users/username/OneDrive/Desktop/Project Folder/sound.wav', winsound.SND_ASYNC)
So, like the answer I switched the slashes around, but kept the SND_ASYNC and that runs in the program.
try changing the flag shown below and also the backslash to forward slash
winsound.PlaySound("D:\GamesImade\pythonpong\bounce.wav", winsound.SND_ASYNC)
to
winsound.PlaySound("D:/GamesImade/pythonpong/bounce.wav", winsound.SND_FILENAME)
Related
I am making a hangman python game to get to know the python syntax but as the code plays the background music .wav file, the code delays until the end of the music instead of running simultaneously.
I have tried using the os.system command (from the os library) and also subprocess.popen() in order to open the music in a new script window using a different script file but nothing worked.
import os
def music(): #this is the in-game function that is supposed to run the music
os.system("musicforhangmaninit.py")
import winsound #this is the command that plays the .wav file music created earlier by this script
winsound.PlaySound('hangmanadditionals.wav', winsound.SND_FILENAME)
no error messages appeared but I think that I got the os.system() function wrong because it seems to do nothing at all, the subprocess.popen() played the music but still delayed the game. (the goal output was the music playing simultaneously with the game I made)
P.s. : I prefer a solution that works on all the operating systems or at least Linux and windows
Recently I tried to implement a "Beep" sound to my code, I tried it with winsound.Beep(), but I have the beep disabled on my computer. Then i tried it with winsound.PlaySound('Test.wav', winsound.SND_FILENAME ), but when I run the code I only hear the windows background sound and not the actual sound.
winsound.PlaySound() is always going to play something, unless you explicitly add the SND_NODEFAULT flag. Your Test.wav file apparently doesn't exist in the current directory, or isn't in a format that Windows can read, so you're getting a default sound instead.
I am trying to make my own music player with Python, and after looking at alternatives, I've settled on using pygame's mixer.music to actually play the audio. (I've used pygame before, just usually for actual games) I was looking at playsound instead until I realized I needed a way to play the next song once one is done, as well as the ability to play and pause the audio. I also need to play mp3 files instead of the wavs that most alternatives require.
I actually got it working perfectly originally, until I tried adding other unrelated features, and now it's saying:
File "main.py", line 66, in playCurrentSong
mixer.load(path.join(museDir, currentSong))
pygame.error: Error reading the stream. (code 18)
(museDir is my variable for the directory that music files are in, and mixer is my variable for pygame.mixer.music as a shorthand)
I cannot figure out for the life of me why it's giving me this error now, as it played the audio perfectly fine before. My code is here: https://pastebin.com/V7nAfmK6
If a solution only works on a certain operating system, my final OS will be Linux, on a Rasperry Pi, but I'm trying to write and test the code on Windows. However, if that's not possible, I understand.
Thank you beforehand for any and all help; this is giving me a headache.
I just found the source of the problem.
Before the error popped up, I had been trying to mess around with the metadata of the mp3 files in order to incorporate a genre system into the player, but nothing was working. I eventually decided to use csv files for that instead.
I must have done something wrong however when I was messing around with that metadata because I looked at the mp3s in File Explorer, and they were all 0 bytes. That's why pygame couldn't read the stream: there wasn't one! I plugged the pygame stuff back in, replaced the mp3s with new ones, and it works just fine now.
Thanks for the help anyway though, Torxed!
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.
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 :)