Play Sound In Python
Có thể bạn quan tâm
Play sound on Python is easy. There are several modules that can play a sound file (.wav).These solutions are cross platform (Windows, Mac, Linux).
The main difference is in the ease of use and supported file formats. All of them should work with Python 3. The audio file should be in the same directory as your python program, unless you specify a path.
Let’s explore the options!
Related course: Complete Python Programming Course & Exercises
Play sound in Python
playsound module
The playsound module is a cross platform module that can play audio files. This doesn’t have any dependencies, simply install with pip in your virtualenv and run!
12 | from playsound import playsoundplaysound('audio.mp3') |
Implementation is different on platforms. It uses windll.winm on Windows, AppKit.NSSound on Apple OS X and GStreamer on Linux.
I’ve tested this with Python 3.5.3. This should work with both WAV and MP3 files.
pydub
You can play sound files with the pydub module. It’s available in the pypi repository (install with pip).This module can use PyAudio and ffmpeg underneath.
12345 | from pydub import AudioSegmentfrom pydub.playback import playsong = AudioSegment.from_wav("sound.wav")play(song) |
snack sound kit
The module snack sound kit can play several audio files: WAV, AU, AIFF, MP3, CSL, SD, SMP, and NIST/Sphere.
You can install it with your package manager: ‘apt install python3-tksnack’. For old versions there’s ‘python-tksnack’.
This module depends on Tkinter. That means that to play sound with this module, you’d also have to import the gui module Tkinter. The module doesn’t seem to have been updated in a while.
123456789 | from Tkinter import *import tkSnackroot = Tk()tkSnack.initializeSnack(root)snd = tkSnack.Sound()snd.read('sound.wav')snd.play(blocking=1) |
native player
You can also play sounds natively on your system. This requires you to have some kind of audio player installed on the terminal. On Linux you can use mpg123 for that.
This simply plays the mp3 file with an external player.
123456 | # apt install mpg123import osfile = "file.mp3"os.system("mpg123 " + file) |
Related course: Complete Python Programming Course & Exercises
Từ khóa » Thư Viện Playsound Python
-
Phát âm Thanh Bằng Python - HelpEx
-
Playsound - PyPI
-
Trợ Lý Ảo | Sửa Lỗi Khi Nói Trong Thư Viện Playsound - YouTube
-
Hướng Dẫn Làm Trợ Lý ảo đơn Giản 2020 Với Python (phần 3/4)
-
Xây Dựng Trợ Lý ảo Tiếng Việt Bằng Ngôn Ngữ Python (cơ Bản)
-
Winsound — Sound-playing Interface For Windows — Python 3.10.5 ...
-
Play A Sound With Python - Audio - Stack Overflow
-
Một Việc Là Chương Trình Spyder Có Thể Chèn Nhạc Vào Không ?
-
Lập Trình Trợ Lý Ảo Tiếng Việt Toàn Diện Với Python - CodeLearn
-
Hướng Dẫn Các Bước Tạo đồng Hồ Báo Thức Bằng Ngôn Ngữ Python
-
Thư Viện cx - Thư Viện Os Trong Python Thư Viện Os...
-
#1 Trợ Lý Ảo | Sửa Lỗi Khi Nói Trong Thư Viện Playsound - Vzone
-
Tạo Trợ Lý Giọng Nói Bằng Python Và đó Là Các Thư Viện
-
Cách Thêm âm Thanh Vào Video Trong Python Bằng Thư Viện MoviePy