4

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

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
akeg
  • 151
  • 2
  • 14

4 Answers4

2

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.

GLaDOS
  • 308
  • 1
  • 10
2

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)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Yibing Ge
  • 21
  • 1
0

Simply call set_pos() after play().

0

I was able to get pausing and resuming to work, by using the start parameter in the Play function:

mixer.music.play(start=time)

Utilizing the start position in the Play function will give you the desired result.

Example Basic Code:

from pygame import mixer
from time import sleep

filename = '/Users/JayRizzo/github/MP3Player/Music/We Swarm.mp3'

mixer.init()                    # initialize the mixer
mixer.music.load(filename)      # load file to mixer
mixer.music.play()              # play song
sleep(3)                        # Sleep For  3 seconds to showcase playing some of the song then,
mixer.music.pause()             # pause song
print(f"Seconds Playing: {mixer.music.get_pos()/1000}")
TimeMs = mixer.music.get_pos()  # Set time variable with milliseconds from get_pos()
TimeS = TimeMs / 1000           # Update to seconds per yibing-ge's point. https://stackoverflow.com/a/53201095/1896134
sleep(3)                        # Sleep For  3 seconds to showcase pausing the song then,
print(f"Seconds Playing: {mixer.music.get_pos()/1000}")
mixer.music.play(start=TimeS)   # Continue song from last Postition
sleep(10)                       # Sleep For 10 seconds to showcase resume functionality
print(f"Seconds Playing: {mixer.music.get_pos()/1000}")
#                               # proving the use of the "mixer.music.play(start=self.time)" work by continuing the song.

# Example Result:
# pygame 2.1.2 (SDL 2.0.18, Python 3.10.5)
# Hello from the pygame community. https://www.pygame.org/contribute.html
# Seconds Playing: 2.995
# Seconds Playing: 2.995
# Seconds Playing: 10.008
# [Finished in 16.4s]

Example Class Code:

from pygame import mixer
from time import sleep


class Songz(object):
    """Showcase Resuming a song in PyGame Mixer."""
    def __init__(self):
        super(Songz, self).__init__()
        self.filename = '/Users/JayRizzo/github/MP3Player/Music/We Swarm.mp3'
        self.time = 0
        mixer.init()
        mixer.music.load(self.filename)

    def songPlay(self):
        mixer.music.play()

    def songPause(self):
        mixer.music.pause()
        self.time = mixer.music.get_pos()
        # Update to seconds per yibing-ge's point. https://stackoverflow.com/a/53201095/1896134
        self.time = self.time / 1000
        print(f"self.time: {self.time}")

    def songUnPause(self):
        mixer.music.play(start=self.time)
        print(f"self.time: {self.time}")

if __name__ == '__main__':
    a = Songz()
    a.songPlay()
    sleep(3)   # Sleep For  3 seconds to showcase playing some of the song then,
    a.songPause()
    sleep(3)   # Sleep For  3 seconds to showcase pausing the song then,
    a.songUnPause()
    sleep(10)  # Sleep For 10 seconds to showcase resume functionality
    # proving the use of the "mixer.music.play(start=self.time)" work by continuing the song.
JayRizzo
  • 3,234
  • 3
  • 33
  • 49