I have a while loop
for my cameras[with opencv] to take a photos when something moves. I would like to call a function to play a sound as well. But when I call and play it, it will stop looping for that execution time. I tried ThreadPoolExecutor
but had no idea how could I blend it with my code, because I'm not passing anything to the function. Just calling it from loop. Btw. I would like to be able to play it multiple times [multiple executions in time of execution] if multiple something
in
code appears from loop
camera script
from play_it import alert
while True:
#do something in cv2
if "something":
alert[] # Here it slowing the loop
and my play_it
script
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
asked Nov 26, 2019 at 1:23
3
I don't know what requirements playsound
has for the thread it runs on, but the simplest and easiest thing to do is probably just to spawn off a thread to play the sound:
import threading
def alert[]:
threading.Thread[target=playsound, args=['ss.mp3',], daemon=True].start[]
daemon=True
here starts the thread as a daemon thread, meaning that it won't block the program from exiting. [On Python 2, you have do t = threading.Thread[...]; t.daemon = True; t.start[]
instead.]
answered Nov 26, 2019 at 3:19
nneonneonneonneo
165k35 gold badges294 silver badges368 bronze badges
0
In this article, we will see how to play sound in Python using some of the most popular audio libraries. We will learn about the various methods for playing sound.
Method 1: Using playsound module
Run the following command to install the packages:
pip install playsound
- The playsound module contains only a single function named playsound[].
- It requires one argument: the path to the file with the sound we have to play. It can be a local file, or a URL.
- There’s an optional second argument, block, which is set to True by default. We can set it to False for making the function run asynchronously.
- It works with both WAV and MP3 files.
Example: For WAV format
Python3
from
playsound
import
playsound
playsound[
'/path/note.wav'
]
print
[
'playing sound using playsound'
]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134813/gfgplaysound.mp4
Example: For mp3 format
Python3
from
playsound
import
playsound
playsound[
'/path/note.mp3'
]
print
[
'playing sound using playsound'
]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134813/gfgplaysound.mp4
Method 2: Using pydub module
Run the following commands to install the packages:
sudo apt-get install ffmpeg libavcodec-extra pip install pydub
Note: You can open WAV files with python. For opening mp3, you’ll need ffmpeg or libav.
This module uses the 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:
Example 1: For WAV format
Python3
from
pydub
import
AudioSegment
from
pydub.playback
import
play
song
=
AudioSegment.from_wav[
"note.wav"
]
print
[
'playing sound using pydub'
]
play[song]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134814/gfgpydub.mp4
Example 2: For mp3 format
Python3
from
pydub
import
AudioSegment
from
pydub.playback
import
play
song
=
AudioSegment.from_mp3[
"note.mp3"
]
print
[
'playing sound using pydub'
]
play[song]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134814/gfgpydub.mp4
Method 3: Using tksnack module
The tksnack 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
The play[] method is used to play the audio files. The blocking argument states that the sound will play asynchronously.
Example:
Python3
from
Tkinter
import
*
import
tkSnack
root
=
Tk[]
tkSnack.initializeSnack[root]
snd
=
tkSnack.Sound[]
snd.read[
'note.wav'
]
print
[
'playing sound using tkSnack'
]
snd.play[blocking
=
1
]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134818/gfgtksnack.mp4
Method 4: Using Native Player
In this method, we play sounds natively on our system. This method plays the audio file with an external player installed on your terminal.
Example 1: For Mac OS X
Python3
import
os
file
=
"note.wav"
print
[
'playing sound using native player'
]
os.system[
"afplay "
+
file
]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134811/gfgnativeplayer.mp4
Example 2: For Linux
Python3
import
os
file
=
"note.mp3"
print
[
'playing sound using native player'
]
os.system[
"mpg123 "
+
file
]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134811/gfgnativeplayer.mp4
Method 5: Using simpleaudio module
This is mainly designed to play WAV files and NumPy arrays. Run the following command to install the packages:
$ sudo apt-get install libasound2-dev $ pip3 install simpleaudio
The play[] method is used to play the audio files.
Example:
Python3
import
simpleaudio as sa
wave_object
=
sa.WaveObject.from_wave_file['note.wav]
print
[
'playing sound using simpleaudio'
]
play_object
=
wave_object.play[]
play_object.wait_done[]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210102134816/gfgsimpleaudio.mp4