How can I play an audio file in the background using Python

+2 votes
I'm just making a small game using pygame and I've been wanting to add some background music. Can someone guide me?
Apr 4, 2018 in Python by aryya
• 7,460 points
13,551 views

3 answers to this question.

+2 votes


down voteacceptedFor windows:

you could use  winsound.SND_ASYNC to play them asynchronously

import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )

To stop playing

winsound.PlaySound(None, winsound.SND_ASYNC)

for other platforms: You can try this Pygame/SDL

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()


Hope this will help!

To learn more, go for Python Master course today.

Thank!

answered Apr 4, 2018 by charlie_brown
• 7,720 points

THANX

don't forget this:

import winsound
winsound.PlaySound("filename"....
only open .wav files
Hello, can you please tell me that, where to store the file while using in my game because I saved it in desktop and I used it in my program and it shows an error.

Hi, @There,

Could you please post the error you are facing? Please post your workaround so that it gets easier to resolve the bug.

0 votes

In windows:

Use winsound.SND_ASYNC to play them asynchronously

import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )

To stop playing

winsound.PlaySound(None, winsound.SND_ASYNC)

In mac or other platforms: You can try this Pygame/SDL

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
answered Oct 12, 2018 by findingbugs
• 4,780 points
0 votes

Playsound has an option for running in the background -> additional argument to be set to 0:

playsound('xxxxx.mp3", **0**)
answered Oct 12, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,770 points
2,393 views
0 votes
1 answer

how can i extact all the links from a website using python and save it in a csv file ?

Hi, @Shubham, Web scraping is the technique to ...READ MORE

answered Jun 16, 2020 in Python by Gitika
• 65,770 points
4,106 views
0 votes
1 answer

How can I lookup hostname using the IP address with a timeout in Python?

Good question. I actually was stuck with ...READ MORE

answered Feb 6, 2019 in Python by Nymeria
• 3,560 points
2,570 views
0 votes
0 answers
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,890 points
20,016 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,308 views
0 votes
2 answers

How do play audio (playsound) in background of Python script?

There’s an optional second argument, block, which ...READ MORE

answered Jun 8, 2019 in Python by anonymous
27,666 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,343 views
0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,663 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP