How to pause at any time in pygame.mixer?

153 views Asked by At

I'm currently writing a basic music player (no GUI). I'm trying to find a way to pause the music at any point in the song I'm playing, and skip at any point as well. I've tried keyboard and opencv modules, but there is a small delay, and you have to press at the right time to pause. Is there any way around this? Or is there another module I've missed? I've isolated actual playing of the song in a function, and here it is:

from pygame import mixer
def playsong(songname):
    global songs, filepath
    print('Currently playing: '+songname)
    print('Press \'n\' to skip\nand \'p\' to pause and unpause')
    mixer.init()  # initiate the mixer instance
    mixer.music.load(filepath + songname + '.mp3')
    mixer.music.play()  # plays the music

My current code to pause and skip the music is:

while mixer.music.get_busy():
    if keyboard.is_pressed('n'):
        mixer.music.stop()
        break
    if keyboard.is_pressed('p'):
        mixer.music.pause()
       
0

There are 0 answers