Categories
Python Answers

How to play a sound with Python?

Spread the love

Sometimes, we want to play a sound with Python.

In this article, we’ll look at how to play a sound with Python.

How to play a sound with Python?

To play a sound with Python, we can use the pyglet module.

To install it, we run

pip install pyglet

Then, we write

import pyglet

music = pyglet.resource.media('music.mp3')
music.play()

pyglet.app.run()

to call pyglet.resource.media to open the music.mp3 file.

Then we call music.play to play the file.

Conclusion

To play a sound with Python, we can use the pyglet module.

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 *