Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame.error: set_pos unsupported for this codec

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).

like image 798
akeg Avatar asked Oct 14 '25 16:10

akeg


2 Answers

Not enough rep to comment.

unfortunately pygame.mixer.music.set_pos() is only supported after v 1.9.2. As you have 1.9.3, it is supposed to run in your code. I can't find out the reason but I can offer you an alternative:

play_time = pygame.mixer.music.get_pos()

This will give you playtime in milliseconds. type of 'play_time' is 'int'. Initialize a variable 'start = 0'. You can now use

start = start + play_time/1000.0

everytime you pause() or stop() to get time in seconds. Why add it to previous value is because if you don't add 'play_time/1000.0' to the previous value of start, then get_pos() will only calculate the time that passed since you started playing and that won't give you the current position at which playback is paused or stopped but only time passed since when the last playback began.

Now you can do this before or after

pygame.mixer.music.pause() #or stop()

You will now have the time at which it paused.

Instead of using set_pos(), go for

pygame.music.play(-1, start)

note that play(loop, start_pos) take start_pos in seconds. so we needed it in secs. you can now remove set_pos()

Alternative: simply use

pygame.mixer.music.pause()
pygame.mixer.music.unpause()

you don't need to calculate time and then set it again.

like image 149
GLaDOS Avatar answered Oct 17 '25 04:10

GLaDOS


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)
like image 34
Yibing Ge Avatar answered Oct 17 '25 04:10

Yibing Ge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!