0

I want to play a sound file for a certain period of time and stop playing it after this time period. I used the playsound function of the playsound python module. I created a process with playsound as target (module multiprocessing) in order to be able to terminate this process. Starting the process works fine, sound begins to play. But if I wait e.g. 5 seconds with time.sleep(5) and terminate the process, the sound keeps playing. Here is my code:

import multiprocessing
from playsound import playsound
import time


if __name__ == "__main__":
    p = multiprocessing.Process(target=playsound, args=('my_soundfile.mp3',))
    p.start()
    time.sleep(5)
    p.terminate()

So if I run this code, the sound file starts to play, but it does not stop playing after 5 seconds. The sleep time period does not matter, same problem with other values. If I terminate the process immediately after starting it

p.start()
p.terminate()

the sound file does not start playing, I assume because terminating the process works as intended. But if I do something (e.g. waiting a period of time with time.sleep()) in between, the sound file keeps playing.

I have the idea of using multiprocessing in order to stop the sound file from playing from this post: How to stop audio with playsound module? (I wanted to ask my question as a comment in this post, but I need a reputation of 50 for that...)

I know there are other ways to play a sound file in python, but I want to know why terminating this process does not stop the sound file from playing.

I'm using Ubuntu 22.04 and Python 3.10.6

  • I would guess that `playsound()` on your platform works by launching a separate sound-playing utility. You have no control over that process, because you have no way of even knowing which process that is. You need to use some other approach that, unlike `playsound`, is actually designed to give you ongoing control over the sound being played - `pygame`, perhaps. – jasonharper Mar 24 '23 at 14:20
  • `pyaudio` would be another possible option. That being said, instead of trying to play just 5 seconds of a larger sound file, it would probably be better to make a clip of the source file that is the length you want to play, and then just use that smaller file. From reading the `playsound` docs, it also looks like you could avoid multiprocessing by passing the `block=False` optional parameter to `playsound()` – nigh_anxiety Mar 24 '23 at 22:06

1 Answers1

0

As I want to mark this question as solved, here is the summary so far:

  • As @jasonharper suggested, the problem here is that playsound() launches another program that plays the sound, terminating the original process won't have any effect on this new process.
  • Possible solutions for this problem are (from the comments of @nigh_anxiety and @jasonharper):
    • use something else like pyaudio (which I did, works fine) or pygame
    • Shorten the original soundfile to the length I want it to play (that would work, but too much effort...)