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 ariaholic
• 7,320 points
1,289 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,710 points
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,750 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
• 56,160 points

Related Questions In Python

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 in Python by Nymeria
• 3,500 points
56 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,810 points
87 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,420 points
68 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 in Python by anonymous
2,338 views
0 votes
1 answer

How can I rename files on the fly using Python?

You could simply use a wrapper object ...READ MORE

answered Sep 7, 2018 in Python by ariaholic
• 7,320 points
32 views
0 votes
1 answer

How can I define a multidimensional array in python using ctype?

Here's one quick-and-dirty method: >>> A = ((ctypes.c_float ...READ MORE

answered Oct 9, 2018 in Python by ariaholic
• 7,320 points
198 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,710 points
61 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,710 points
53 views