Help with Resuming Music from Last Paused Position

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
fgfg3356
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

Help with Resuming Music from Last Paused Position

#1 Post by fgfg3356 »

I'm integrating a feature into my Ren'Py game that involves pausing and resuming background music exactly where it was left off. This functionality isn't my original creation; I've adapted it from another developer's code.(Reference for the original code: viewtopic.php?t=43406) However, I'm encountering some difficulties in getting it to work as intended, specifically with resuming music from the last paused position.

Below is the adapted code:

Code: Select all

default pauseddict = {}
init python:

    def _insert(name, time):
        store.pauseddict[name] = time

    def fading_pause(channel="music", pause="toggle", fade=None, filename=None):
        if fade is None:
            fade = 0
        if pause == "toggle":
            pause = renpy.music.get_playing(channel)
        
        if pause:
            current_track = renpy.music.get_playing(channel)
            if current_track:
                current_pos = renpy.music.get_pos(channel)
                # Save the current track information
                _insert(current_track, current_pos)
                renpy.music.stop(channel, fade)
        else:
            if filename is None:
                name, time = store.pauseddict.popitem()
            else:
                name, time = store.pauseddict.pop(filename, (None, None))
            if name and time is not None:
                # Check if the <from x> tag already exists to avoid duplication
                if "<from" not in name:
                    fn = "<from {}>".format(time) + name
                else:
                    fn = name
                renpy.music.play(fn, channel=channel, fadein=fade)

The intention is for the music to pause and then resume playing from the exact point of pause, even after multiple pauses. However, the functionality does not seem to work as smoothly as expected, and I'm unsure whether the issue lies with the way pause positions are saved or retrieved.

I'm reaching out to see if anyone can help identify any potential issues with this code or suggest improvements to make it work more effectively. I appreciate any insights or advice you can offer.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1031
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Help with Resuming Music from Last Paused Position

#2 Post by m_from_space »

fgfg3356 wrote: Tue Feb 06, 2024 6:23 pmThe intention is for the music to pause and then resume playing from the exact point of pause, even after multiple pauses. However, the functionality does not seem to work as smoothly as expected, and I'm unsure whether the issue lies with the way pause positions are saved or retrieved.
Code looks fine to me, but I didn't test it. Maybe describe what you think is wrong with it, because it's hard to guess what your actual problem is.

Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot]