pygame.error: set_pos unsupported for this codec

Viewed 2071

I have a problem with my python audio player. I use this function to pause the music that is playing:

def pause(event):
    global time
    pygame.mixer.music.pause()
    time=pygame.mixer.music.get_pos()

And then,I'm trying to play it again from the position where it's stops with this function:

def play(event):
    global time
    name=listbox.get(ACTIVE)
    file="music/"+str(name)
    mixer.music.load(file)
    pygame.mixer.music.play()
    if time >0:
        pygame.mixer.music.set_pos(time)
        mixer.music.play()
    else:    
        mixer.music.play()

But after that I get this error:

pygame.error: set_pos unsupported for this codec

Also I tried pygame.mixer.music.unpause() function:

def play(event):
    global time
    name=listbox.get(ACTIVE)
    file="music/"+str(name)
    mixer.music.load(file)
    if time >0:
        mixer.music.unpause()
    else:    
        mixer.music.play()

But it is simply not working, no errors at all in this case. I use python 3.6 and pygame 1.9.3 on Windows 10(64 bits).

3 Answers

This error is because that the time unit of set_pos() is seconds but get_pos() is millseconds.

So your set_pos() function should pass time/1000 instead of time:

pygame.mixer.music.set_pos(time/1000)

Simply call set_pos() after play().

Related