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 3, 2018 in Python by aryya
• 7,380 points
6,417 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()
answered Apr 3, 2018 by charlie_brown
• 7,780 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,790 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
• 57,700 points

Related Questions In Python

0 votes
1 answer
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 in Python by Gitika
• 41,360 points
362 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,520 points
484 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,870 points
9,857 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
316 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 7, 2019 in Python by anonymous
14,716 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,780 points
347 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,780 points
1,017 views