Hướng dẫn can you make python play sounds? - bạn có thể tạo ra âm thanh chơi python không?

Trong bài viết này, chúng ta sẽ thấy cách phát âm thanh trong Python bằng cách sử dụng một số thư viện âm thanh phổ biến nhất. Chúng tôi sẽ tìm hiểu về các phương pháp khác nhau để phát âm thanh.

Phương pháp 1: Sử dụng mô -đun chơiUsing playsound module

Chạy lệnh sau để cài đặt các gói:

pip install playsound
  • Mô -đun chơi chỉ chứa một hàm duy nhất có tên Playsound ().playsound().
  • Nó yêu cầu một đối số: đường dẫn đến tệp với âm thanh chúng ta phải phát. Nó có thể là một tập tin cục bộ, hoặc một URL.
  • Có một đối số thứ hai tùy chọn, khối, được đặt thành true theo mặc định. Chúng ta có thể đặt nó thành sai để làm cho chức năng chạy không đồng bộ.block, which is set to True by default. We can set it to False for making the function run asynchronously.
  • Nó hoạt động với cả tệp WAV và MP3.WAV and MP3 files.

Ví dụ: Đối với định dạng WAVFor WAV format

Python3

from playsound import playsound

playsound('/path/note.wav'

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

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
3
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134813/gfgplaysound.mp4

Ví dụ: cho định dạng MP3For mp3 format

Python3

from playsound import playsound

playsound(

sudo apt-get install python3-tk
sudo apt-get install python3-tksnack
0
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
3
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134813/gfgplaysound.mp4

Ví dụ: cho định dạng MP3Using pydub module

Phương pháp 2: Sử dụng mô -đun Pydub

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

Chạy các lệnh sau để cài đặt các gói:You can open WAV files with python. For opening mp3, you’ll need ffmpeg or libav.

Lưu ý: Bạn có thể mở các tệp WAV bằng Python. Để mở MP3, bạn sẽ cần FFMPEG hoặc LIBAV.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:

Mô -đun này sử dụng phương thức From_wav () để phát tệp WAV và phương thức từ_mp3 () để phát tệp MP3. & nbsp; phương thức play () được sử dụng để phát tệp wav và mp3:For WAV format

Python3

Ví dụ 1: Đối với định dạng WAV

from

sudo apt-get install python3-tk
sudo apt-get install python3-tksnack
7import
sudo apt-get install python3-tk
sudo apt-get install python3-tksnack
9

from

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
1import
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
3

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2from1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

from3

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134814/gfgpydub.mp4

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
4
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
6
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
7
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0
For mp3 format

Python3

Ví dụ 1: Đối với định dạng WAV

from

sudo apt-get install python3-tk
sudo apt-get install python3-tksnack
7import
sudo apt-get install python3-tk
sudo apt-get install python3-tksnack
9

from

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
1import
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
3

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2from1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

from3

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134814/gfgpydub.mp4

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
4
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
6
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
7
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0
Using tksnack module

Ví dụ 2: cho định dạng MP3tksnack 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

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
4
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5 playsound 4playsound 5
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0play() method is used to play the audio files. The blocking argument states that the sound will play asynchronously.

Example: 

Phương pháp 3: Sử dụng mô -đun Tksnack

Mô -đun TKSNack phụ thuộc vào một mô -đun có tên Tkinter để kích hoạt một đối tượng TK trong tập lệnh Python. Bạn phải cài đặt các gói Tkinker và Tksnack cho Python. Chạy các lệnh sau để cài đặt các gói:

Phương thức play () được sử dụng để phát các tệp âm thanh. Đối số chặn nói rằng âm thanh sẽ phát không đồng bộ.

Python3

playsound1

from import3import import5

playsound5playsound6

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

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2playsound(0
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

playsound(2

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5playsound(4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134818/gfgtksnack.mp4

import import7Using Native Player

import8

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5 playsound0natively on our system. This method plays the audio file with an external player installed on your terminal.

playsound2

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5 playsound4 For Mac OS X

Python3

Phương pháp 4: Sử dụng người chơi bản địa

Trong phương pháp này, chúng tôi chơi âm thanh tự nhiên trên hệ thống của chúng tôi. Phương pháp này phát tệp âm thanh với một trình phát bên ngoài được cài đặt trên thiết bị đầu cuối của bạn.

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2'/path/note.wav'3
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

Ví dụ 1: Đối với Mac OS X

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134811/gfgnativeplayer.mp4

import playsound(7For Linux

Python3

Phương pháp 4: Sử dụng người chơi bản địa

Trong phương pháp này, chúng tôi chơi âm thanh tự nhiên trên hệ thống của chúng tôi. Phương pháp này phát tệp âm thanh với một trình phát bên ngoài được cài đặt trên thiết bị đầu cuối của bạn.

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2'/path/note.wav'3
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

Ví dụ 1: Đối với Mac OS X

Output::

https://media.geeksforgeeks.org/wp-content/uploads/20210102134811/gfgnativeplayer.mp4

import playsound(7Using simpleaudio module

playsound(8

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
7WAV files and NumPy arrays. Run the following command to install the packages:

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio

'/path/note.wav'5'/path/note.wav'6 '/path/note.wav'7 playsound(8

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0play() method is used to play the audio files.

Example:

Python3

Ví dụ 2: Đối với Linux

playsound(8

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5 playsound 5

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
2
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
21
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

'/path/note.wav'5

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
10 '/path/note.wav'7 playsound(8
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
0

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

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20210102134816/gfgsimpleaudio.mp4


Làm thế nào để bạn tạo ra một âm thanh python script?

Để tạo ra một âm thanh bíp trong Python, bạn có các tùy chọn sau:..
Sử dụng ký tự chuông trên thiết bị đầu cuối ..
Sử dụng AppKit để phát âm thanh MacOS System ..
Sử dụng Winsound để phát âm thanh hệ thống Windows ..
Sử dụng pygame để phát các tệp âm thanh tùy chỉnh ..
Sử dụng SimpleAudio để phát các tệp âm thanh tùy chỉnh ..
Sử dụng gói Beepy ..

Có một mô -đun âm thanh trong Python?

Playsound là một mô -đun Python mà người dùng có thể phát âm thanh trong một dòng mã duy nhất.Đây là một mô -đun nền tảng chéo là một hàm duy nhất mà không có bất kỳ phụ thuộc nào để phát âm thanh và âm thanh.Ví dụ: từ Nhập khẩu Playsound Playsound.. It is a cross - platform module which is a single function without any dependencies for playing sounds and audios. For example: from playsound import playsound.

Tkinter có thể chơi âm thanh không?

Có hai mô-đun để phát âm thanh với sự trợ giúp của Tkinter Python: Pygame: Đây là một mô-đun đa nền tảng để tạo ra các trò chơi và GUI.Playsound: Đây là mô-đun đa nền tảng và tên hàm của nó là Playsound (): pygame : It is a cross-platform module to create games and GUI's. playsound: It is a cross-platform module and its function name is playsound()