Hướng dẫn python play wav file - python chơi tệp wav

Tôi có một while loop cho máy ảnh của tôi (với OpenCV) để chụp ảnh khi có thứ gì đó di chuyển. Tôi cũng muốn gọi một chức năng để phát một âm thanh là tốt. Nhưng khi tôi gọi và chơi nó, nó sẽ ngừng lặp lại cho thời gian thực hiện đó. Tôi đã thử ThreadPoolExecutor nhưng không biết làm thế nào tôi có thể trộn nó với mã của mình, bởi vì tôi không chuyển bất cứ thứ gì cho chức năng. Chỉ gọi nó từ Loop. BTW. Tôi muốn có thể chơi nó nhiều lần (nhiều lần thực hiện trong thời gian thực thi) nếu nhiều something trong mã xuất hiện từ Loop

Show

tập lệnh camera

from play_it import alert

while True:
    #do something in cv2
    if "something":
        alert() # Here it slowing the loop

và tập lệnh

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
0 của tôi

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here

Đã hỏi ngày 26 tháng 11 năm 2019 lúc 1:23Nov 26, 2019 at 1:23

Hướng dẫn python play wav file - python chơi tệp wav

3

Tôi không biết những yêu cầu

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
1 có đối với chủ đề mà nó chạy, nhưng điều đơn giản nhất và dễ làm nhất có lẽ chỉ là sinh ra một luồng để phát âm thanh:

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
2 Ở đây bắt đầu luồng dưới dạng luồng daemon, nghĩa là nó sẽ không chặn chương trình thoát ra. (Trên Python 2, bạn đã làm
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
3 thay thế.)

Đã trả lời ngày 26 tháng 11 năm 2019 lúc 3:19Nov 26, 2019 at 3:19

Nneonneonneonneonneonneo

165K35 Huy hiệu vàng294 Huy hiệu bạc368 Huy hiệu đồng35 gold badges294 silver badges368 bronze badges

0

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
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
5
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
1

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
8
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
9
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
3
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

Output:

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

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

Python3

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
5
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
1

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
8
pip install playsound
0
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
3
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

Output:

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

https://media.geeksforgeek.org/wp-content/uploads/20210102134813/gfgplaysound.mp4Using pydub module

Ví dụ: cho định dạng MP3

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

Phương pháp 2: Sử dụng mô -đun PydubYou can open WAV files with python. For opening mp3, you’ll need ffmpeg or libav.

Chạy các lệnh sau để cài đặt các gói: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:

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.For WAV format

Python3

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:

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

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4
pip install playsound
7
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
pip install playsound
9

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
3

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

Output:

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

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
6
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
7
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0
For mp3 format

Python3

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:

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

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4
pip install playsound
7
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
pip install playsound
9

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2
sudo apt-get install python3-tk
sudo apt-get install python3-tksnack
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

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

Output:

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

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
3
Using tksnack module

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
6
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
7
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0tksnack 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

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1______22221play() method is used to play the audio files. The blocking argument states that the sound will play asynchronously.

Example: 

Ví dụ 2: cho định dạng MP3

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
4
$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio
5
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

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:

ThreadPoolExecutor1

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ộ.

ThreadPoolExecutor5ThreadPoolExecutor6

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2something0
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

something2

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5something4
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

Output:

Python3

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
4 while loop3
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6 while loop5
Using Native Player

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6 while loop7natively on our system. This method plays the audio file with an external player installed on your terminal.

while loop8

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5 ThreadPoolExecutor0 For Mac OS X

Python3

ThreadPoolExecutor2

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

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

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
03
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

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

Output:

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.

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

Python3

ThreadPoolExecutor2

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

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

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
03
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

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

Output::

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

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.Using simpleaudio module

Điều này chủ yếu được thiết kế để phát các tệp WAV và mảng numpy.Chạy lệnh sau để cài đặt các gói:WAV files and NumPy arrays. Run the following command to install the packages:

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

Phương thức play () được sử dụng để phát các tệp âm thanh.play() method is used to play the audio files.

Example:

Python3

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
6
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
25

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
26
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
28

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
1
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
2
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
31
import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
0

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
33
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5
from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
35

from playsound import playsound
import concurrent.futures

def alert():
    playsound('ss.mp3')


def PlayIt():
    with concurrent.futures.ThreadPoolExecutor() as exe:
        exe.map(alert, ???) # not sure what to insert here
36

Output:

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