Ren'Py A little bit of help playing an audio file within Ren'Py Please

Amahl Farouk

Well-Known Member
May 13, 2018
1,320
2,436
Hi Friends,

I am trying to set up ren'py to play an audio file, and have a progress bar showing elapsed time, and time remaining using a colored png.

I have this kind of working but there are a couple of things I can't figure out, and was hoping to get your help

1634207818733.png

So as you can see it is playing the file, and displaying how much of the file has already been listened to.


Here is the code for the screens:

Code:
screen audio_duration():
    bar:
        value AudioPositionValue(channel='music',update_interval=0.1)
        xalign 0.5
        yalign 0.5
        xsize 500
        xmaximum 500
and this is the code for the script


Code:
define Dark = "audio/dark.ogg"
image BG peach="bg peach.jpg"

## Start
label start:
    scene BG peach with dissolve
    jump Dark

label Dark:
    play music Dark noloop
    #$ x=renpy.music.get_duration(channel=u'music')
    show screen audio_duration

label end:
    "Goodbye"
    return

There are a couple of things I would like to do, and one that I really need to do. As you can see from the screenshot, once we jump to Dark, it runs the audio and shows the duration bar, but then it immediately executes the end routine too

1. I really need to delay this until the audio file has stopped. I only put this in because I realized I didn't have full control over the audio file yet

2. The second thing I would like to be able to do is to grab the remaining duration of the file in seconds and assign this value to a variable that I could then display on the screen alongside the progress bar and this number would also change as the file plays through, probably as time already elapsed. The audio files I plan on using are very long files so it would be helpful for the people who will use this game to see how much time in total it will take to listen to all the files, and how much time has already elapsed in the current file

I have really tried to find this info myself and have tried all sorts of things, but I am at the very beginning of learning ren'py so basically, I have no idea how to do it. I experimented with capturing the duration and assigning that duration to a variable , but was getting a 0.0 value so I am probably not doing it right
renpy.music.get_duration(channel=u'music') is what I was trying to use

Can some kind soul please help explain what I should be doing? And remember please, a complicated answer will likely confuse me so keep it simple if you can!
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,368
15,282
once we jump to Dark, it runs the audio and shows the duration bar, but then it immediately executes the end routine too
Because it's what you told Ren'py to do.
Showing a screen do not interrupt the script, therefore once it's done, Ren'py pass to the following instruction. Like there isn't in your script, it pass to the following label and proceed it.


1. I really need to delay this until the audio file has stopped. I only put this in because I realized I didn't have full control over the audio file yet
Like you guessed, the answer is probably in . It's possible that you got a value of 0.0 because of Ren'py optimization made it being called before the music effectively started.
Unless the function is broke, delaying a little more should works:
Code:
    show screen audio_duration
    $ renpy.pause( renpy.music.get_duration() )
At worse, there's an issue with the default value and renpy.music.get_duration( channel="music") should do it.

Alternatively it's perhaps possible to tweak the screen to use .
Something that would looks like:
Code:
screen audio_duration():
    bar:
        value AudioPositionValue(channel='music',update_interval=0.1)
        xalign 0.5
        yalign 0.5
        xsize 500
        xmaximum 500

    timer 0.1 repeat True action If( renpy.music.get_pos() is None, Return(), NullAction() )

label whatever:
    play music Dark noloop
    call screen audio_duration
    [whatever you want]
Alternatively if you don't want to froze the game, you can probably use something like that:
Code:
screen audio_duration():
    bar:
        value AudioPositionValue(channel='music',update_interval=0.1)
        xalign 0.5
        yalign 0.5
        xsize 500
        xmaximum 500

    timer 0.1 repeat True action If( renpy.music.get_pos() is None, Hide( "audio_duration" ), NullAction() )

label whatever:
    play music Dark noloop
    show screen audio_duration
    [whatever you want]
In both case it's possible to rely on a second timer (and a flag) to delay the moment the first one will effectively start to works. Ensuring then that the music will effectively have started.


2. The second thing I would like to be able to do is to grab the remaining duration of the file in seconds and assign this value to a variable
timeLeft = renpy.music.get_duration() - renpy.music.get_pos()
You can't use this directly, since renpy.music.get_pos() return None if there's no music played, but it's the spirit.
 
  • Red Heart
Reactions: Lou111