How to stop audio in python ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

How to stop audio in python ?

Please introduce a module that I can use it in playing a sound and then stop it . I used winsound and multiprocessing modules to do this but it didn't work.

18th Mar 2024, 6:20 PM
Ppsh
5 Réponses
+ 1
import pygame import time pygame.init() pygame.mixer.init() sound = pygame.mixer.Sound('example.wav') channel = sound.play() # Wait for a few seconds time.sleep(2) # Stop the sound channel = sound.stop() P.S. PyGame dont supported in a SoloLearn
18th Mar 2024, 6:31 PM
Knight
Knight - avatar
0
Knight , I haven't used pygame, but the code looks inconsistent in the calling syntax. Are you sure you aren't supposed to do one of these instead? sound.stop() or channel = sound.stop() ?
19th Mar 2024, 10:55 AM
Rain
Rain - avatar
0
Rain oh. My bad. Sorry
19th Mar 2024, 11:15 AM
Knight
Knight - avatar
0
For winsound, you should be able to use the following to stop the current sound from playing: winsound.Playsound(None, winsound.SND_PURGE) If you just need to stop a sound and immediately start playing another sound, then you can just call the next sound to play. You should be using the SND_ASYNC flag whenever you make the call to play your sound(s). This is all assuming that you're running this code on a Windows OS. With multiprocessing there isn't an option to stop the sound. It will play until the file ends, unless you Kill/terminate the process.
20th Mar 2024, 3:38 AM
ChaoticDawg
ChaoticDawg - avatar
0
Can Anyone Explain Me MySQL From Basics
20th Mar 2024, 4:58 PM
Adarsh Kahar
Adarsh Kahar - avatar