Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

I am just writing a small python game for fun and I have a function that does the beginning narrative.

I am trying to get the audio to play in the background but unfortunately the mp3 file plays first before the function continues.

How do I get it to run in the background?

import playsound

def displayIntro():

playsound.playsound('storm.mp3',True)

print('')

print('')

print_slow('The year is 1845, you have just arrived home...')

Also, is there any way of controlling the volume of the play sound module?

I should add that I am using a Mac, and I am not wedded to using playsound, it just seems to be the only module that I can get working.

2 Answers

0 votes
by (33.1k points)

You can the following methods for different operating systems to play audio using the python script.

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: 

pygame.mixer.init()

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

pygame.mixer.music.play()

Hope this answer helps you! Thus, studying Machine Learning tutorials will be of quite important use. Also, undergoing a Python Training is also quite important.

0 votes
by (140 points)
you can use the folloint

import playsound

playsound.playsound('storm.mp3', False)

False makes the audio to run in background

Browse Categories

...