Categories
Python Answers

How to play an mp3 with Python pygame?

Spread the love

Sometimes, we want to play an mp3 with Python pygame.

In this article, we’ll look at how to play an mp3 with Python pygame.

How to play an mp3 with Python pygame?

To play an mp3 with Python pygame, we can call the pygame.time.Clock().tick method in the while loop that we use the return value of pygame.mixer.music.get_busy as the condition.

For instance, we write

while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)

to keep the clip playing as long as pygame.mixer.music.get_busy returns True, which is when the clip is still being played.

Conclusion

To play an mp3 with Python pygame, we can call the pygame.time.Clock().tick method in the while loop that we use the return value of pygame.mixer.music.get_busy as the condition.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *