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
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ôifrom 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
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
5from 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
1from 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
8from 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
9import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
3import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0Output:
//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
5from 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
1from 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
8pip install playsound0
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
3import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134813/gfgplaysound.mp4
//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 playsound7
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 playsound9
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 pydub1
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 pydub3
sudo apt-get install python3-tk sudo apt-get install python3-tksnack3
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134814/gfgpydub.mp4
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
4sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5 sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
6sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
7
0For mp3 formatimport threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
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 playsound7
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 playsound9
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2sudo apt-get install python3-tk sudo apt-get install python3-tksnack1
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0sudo apt-get install python3-tk sudo apt-get install python3-tksnack3
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134814/gfgpydub.mp4
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
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
1
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
sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
3Using tksnack module
sudo apt-get install ffmpeg libavcodec-extra pip install pydub4
sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
sudo apt-get install ffmpeg libavcodec-extra pip install pydub6
sudo apt-get install ffmpeg libavcodec-extra pip install pydub7
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 pydub4
sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
$ sudo apt-get install libasound2-dev $ pip3 install simpleaudio4
$ sudo apt-get install libasound2-dev $ pip3 install simpleaudio5
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0Phươ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:
ThreadPoolExecutor
1
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ộ.
ThreadPoolExecutor
5ThreadPoolExecutor
6
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2something
0import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0something
2
sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
something
4import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0Output:
Python3
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
while loop
3
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
while loop
5Using 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 loop
7natively on our system. This method plays the audio file with an external player installed on your terminal.while loop
8sudo apt-get install ffmpeg libavcodec-extra
pip install pydub
5 ThreadPoolExecutor
0 For Mac OS X
Python3
ThreadPoolExecutor
2
sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
ThreadPoolExecutor
4//media.geeksforgeek.org/wp-content/uploads/20210102134818/gfgtksnack.mp4
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2from 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
03import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0Phươ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
ThreadPoolExecutor
2
sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
ThreadPoolExecutor
4//media.geeksforgeek.org/wp-content/uploads/20210102134818/gfgtksnack.mp4
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2from 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
03import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0Phương pháp 4: Sử dụng người chơi bản địa
Output::
//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
25from 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
26sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
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
28import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
1import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
2from 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
31import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
0from 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
33sudo apt-get install ffmpeg libavcodec-extra pip install pydub5
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
35from 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
36Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134816/gfgsimpleaudio.mp4