Hướng dẫn python play sound async
I have a
asked Nov 26, 2019 at 1:23
I don't know what requirements
answered Nov 26, 2019 at 3:19
165k35 gold badges294 silver badges368 bronze badges
In this article, we will see how to play sound in Python using some of the most popular audio libraries. We will learn about the various methods for playing sound.
Method 1: Using playsound module
Run the following command to install the packages:
pip install playsound
Example: For WAV format
Example: For mp3 format
Method 2: Using pydub module
Run the following commands to install the packages:
sudo apt-get install ffmpeg libavcodec-extra pip install pydub
Note: You can open WAV files with python. For opening mp3, you’ll need ffmpeg or libav.
This module uses the from_wav() method for playing wav file and from_mp3() method for playing an mp3 file. The play() method is used to play the wav and mp3 file:
Example 1: For WAV format
Example 2: For mp3 format
Method 3: Using tksnack module
The tksnack module depends upon a module named tkinter to activate a tk object in the python script. You must install tkinker and tksnack packages for Python. Run the following commands to install the packages:
sudo apt-get install python3-tk sudo apt-get install python3-tksnack
The play() method is used to play the audio files. The blocking argument states that the sound will play asynchronously.
Method 4: Using Native Player
In this method, we play sounds natively on our system. This method plays the audio file with an external player installed on your terminal.
Example 1: For Mac OS X
Example 2: For Linux
Method 5: Using simpleaudio module
This is mainly designed to play WAV files and NumPy arrays. Run the following command to install the packages:
$ sudo apt-get install libasound2-dev $ pip3 install simpleaudio
The play() method is used to play the audio files.