How to play audio with Python?

0 Shares
0
0
0
0

Introduction

In this article, we will see how to play audio in Python using some of the most popular audio libraries. We will learn about different methods of playing audio.

Method 1: Using the playsound module

To install the packages, run the following command:

pip install playsound

 

  • The playsound module only contains one function called playsound().
  • It takes one argument: the path to the file with the sound we want to play. This can be a local file or a URL.
  • There is an optional second argument, block, which is set to True by default. We can set it to False to execute the function asynchronously.
  • Works with both WAV and MP3 files.
# import required module from playsound import playsound # for playing note.wav file playsound('/path/note.wav') print('playing sound using playsound')

روش 2: استفاده از ماژول pydub

Run the following commands to install the packages:

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub

این ماژول از متد from_wav() برای پخش فایل wav و از متد from_mp3() برای پخش فایل mp3 استفاده می کند. متد play() برای پخش فایل های wav و mp3 استفاده می شود:

# import required module from pydub import AudioSegment from pydub.playback import play # for playing mp3 file song = AudioSegment.from_mp3("note.mp3") print('playing sound using pydub') play(song)

روش 3: استفاده از ماژول tksnack

The tksnack module depends on a module called tkinter to enable a tk object in a Python script. You need to install the tkinter and tksnack packages for Python. To install the packages, run the following commands:

sudo apt-get install python3-tk
sudo apt-get install python3-tksnack

از متد play() برای پخش فایل های صوتی استفاده می شود. آرگومان مسدود کننده بیان می کند که صدا به صورت ناهمزمان پخش می شود.

# import required modules
from Tkinter import *
import tkSnack

# initialize tk object to use tksnack
root = Tk()
tkSnack.initializeSnack(root)

# play sound
snd = tkSnack.Sound()
snd.read('note.wav')
print('playing sound using tkSnack')
snd.play(blocking=1)

Method 4: Using Native Player

In this method, we play the sounds natively on our system. This method plays the audio file with an external player installed on your terminal.

# import required module
import os

# play sound
file = "note.wav"
print('playing sound using native player')
os.system("afplay " + file)

روش 5: استفاده از ماژول ساده صوتی

It is mainly designed for playing WAV files and NumPy arrays. To install the packages, run the following command:
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio

از متد play() برای پخش فایل های صوتی استفاده می شود.

# import required module
import simpleaudio as sa

# define an object to play
wave_object = sa.WaveObject.from_wave_file('note.wav)
print('playing sound using simpleaudio')

# define an object to control the play
play_object = wave_object.play()
play_object.wait_done()g
Leave a Reply

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

You May Also Like