Is there a way to play background music in python pgzero? - python

I currently have an audio file in the correct directory but I can't seem to find a command that will play and loop the song. Is it the same as just playing the sound? If not, what is the syntax to play the music?

Pygame Zero seems to make this easy. The methods for playing longer music tracks are different than those for playing short sound clips.
If you want to play a music file called tune1 place it inside a directory below your code file called "music" with the file name tune1.mp3 or tune1.ogg.
Inside your program running the following will play the music on an infinite loop
music.play('tune1')
References:
Build-ins - Music

Related

Python: Background music playing like youtube premieum

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

playing python game background-music without delaying the actual game

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

Pygame works with two of my .wav files but when adding one .wav it crashes?

I am new to programming, and this is my first time trying to make a game. I have two sound effects so far. One plays whenever my ship fires, and it works fine, the other plays a sound when all the enemies are destroyed.
I am trying to play a .wav file every time an enemy ship comes into contact with mine. I am using the same method to load/play these sounds but for whatever reason it crashes unexpectedly. The .wav file making it crash isn't longer than the others.
Here is what I have just for the sound portion.
pygame.init()
destroy = pygame.mixer.Sound('explosion.wav')
fires = pygame.mixer.Sound('laser.wav')
hit = pygame.mixer.Sound('oops.wav')
Later in my lines of code I call the sounds using something like this:
soundname.play(0, 0)
Why would it work fine with the first two sounds but not the third one (the hit sound)? They are all .wav files and all about the same length.

How to use streaming with pygame.mixer [duplicate]

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!

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.

Categories

Resources