Play/Pause audio with textbutton?

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
User avatar
Nanahs
Veteran
Posts: 310
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Play/Pause audio with textbutton?

#1 Post by Nanahs »

I created a menu with a few option that allow the player to play sounds like rain, etc.

Like this:

Code: Select all

    textbutton "Rain" action Play("sound_2", "rainsound.mp3")
When you click the textbutton the audio plays (that's obvious hah).

The thing is, I wanted the same button to stop the audio when clicked again.
I tried adding the "toggle" function, but it didn't work. It kept saying it was not defined.

I could simply create another button called "pause" to pause the audio. But since the audios are in differente channels, that didn't work properly.

What should I do? I just need the button to be a "play/pause" one.

Thanks.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Play/Pause audio with textbutton?

#2 Post by Imperf3kt »

I had a quick look through the documentation https://www.renpy.org/doc/html/audio.html
And there doesn't seem to be a function to toggle audio playing / paused.
The preferences screen has the mute all button, but this stops the audio completely, which means when it starts again, it starts from the beginning.

Perhaps make two buttons in the same location, and determine which to show depending on the state of the playing music?
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Nanahs
Veteran
Posts: 310
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: Play/Pause audio with textbutton?

#3 Post by Nanahs »

Imperf3kt wrote: Tue Mar 05, 2019 2:40 pm I had a quick look through the documentation https://www.renpy.org/doc/html/audio.html
And there doesn't seem to be a function to toggle audio playing / paused.
The preferences screen has the mute all button, but this stops the audio completely, which means when it starts again, it starts from the beginning.

Perhaps make two buttons in the same location, and determine which to show depending on the state of the playing music?
Yes, I guess it there's no function like that.
Anyway, thanks! It'll try to do it :)


User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Play/Pause audio with textbutton?

#5 Post by Imperf3kt »

Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Andredron
Miko-Class Veteran
Posts: 700
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Play/Pause audio with textbutton?

#6 Post by Andredron »

Imperf3kt wrote: Tue Mar 05, 2019 2:40 pm I had a quick look through the documentation https://www.renpy.org/doc/html/audio.html
And there doesn't seem to be a function to toggle audio playing / paused.
The preferences screen has the mute all button, but this stops the audio completely, which means when it starts again, it starts from the beginning.

Perhaps make two buttons in the same location, and determine which to show depending on the state of the playing music?

Code: Select all

init python:
    # reg music canal
    def mplay(fn, chan = "music", fin = 1.0, fout = 1.0):
        renpy.play(fn + ".mp3", channel = chan, loop = True, fadein = fin, fadeout = fout)
    # canal pause
    def mpause(channel = "music"):
        c = renpy.audio.audio.get_channel(channel)
        c.pause()
    # delit pause
    def munpause(channel = "music"):
        c = renpy.audio.audio.get_channel(channel)
        c.unpause()
    # stop canal
    def mstop(chan = "music", fout=1.0):
        renpy.music.stop(channel = chan, fadeout = fout)
# test
label start:
    $ mplay("mus")
    pause (2.0)
    $ mpause()
    pause (2.0)
    $ munpause()
    pause (2.0)
    $ mstop()
    pause (2.0)
    return



Funcion in text button

Code: Select all

$ renpy.vibrate(1.0)

init python:
    def vibrate():
        renpy.vibrate(1.0)

textbutton 'vibtator' action [Function(vibrate), Jump("metka")]



User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Play/Pause audio with textbutton?

#7 Post by Imperf3kt »

I would have thought it'd be easy enough to just do something like this

Code: Select all

 if renpy.music.is_playing(channel='music'):
    #textbutton to pause music
else:
    #textbutton to unpause audio
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Nanahs
Veteran
Posts: 310
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: Play/Pause audio with textbutton?

#8 Post by Nanahs »

Thank you so much everybody! I'll try those codes :)

nanashine
Regular
Posts: 129
Joined: Thu Nov 30, 2017 1:44 pm
itch: renpytom
Contact:

Re: Play/Pause audio with textbutton?

#9 Post by nanashine »

Hum... I got confused abou this.

What "calls" the song menu is this (there's an image of a song icon that shows up on the screen):

Code: Select all

screen songiconbutton():
    
    imagemap:
        ground "songaction.png"
        hover "songaction2.png"

        hotspot(0, 0, 49, 49) action ToggleScreen("audioicon")  
And this is the sound menu:

Code: Select all

screen audioicon():

    add "audiobg.png"
    frame:
        style_group 'status'
        align(0.5, 0.5)
        has vbox 

        
        textbutton "Light rain" action Play("sound_2", "rainsound.mp3")
        null height 10
        textbutton "Storm" action Play("sound_2", "storm.mp3")
        null height 10
        textbutton "Day sound" action Play("sound_3", "daysound.mp3")
        null height 10
        textbutton "Night sound" action Play("sound_3", "nightsound.mp3")
        null height 10
        textbutton "PAUSE ALL" action [PauseAudio('sound_2', value='toggle'), PauseAudio('sound_3', value='toggle')]
        null height 10
As you can see, the sounds are in different channels (so you can combine some of them playing together).

Let's say I was playing "Storm" and "Night sound" together. The first time you choose "PAUSE ALL" it works.
Then you choose another sound, like "Light Rain". When you click "PAUSE ALL", instead of it pausing all, it goes back to playing the previous audios you chose.
I understand why Renpy is doing it, I just don't know how to fix that.

I tried using the codes above but I failed :lol: :oops: :?:

My game I already so full of "difficul codes" that I get confused hah

ps: I decided to remove the "value='toggle'" and now it stops all :)
At first I wanted "toggle" so that the button would be a "pause/unpause" buttom, but I think I will let it work as a "stop button" only. It's less complicated.
I can't access my other account cause I don't remember the e-mail I used *cries in emoji*.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Play/Pause audio with textbutton?

#10 Post by Imperf3kt »

If you want multiple songs playing at the same time, rather than registering one channel per each song, you could use the "audio" channel.

But there are drawbacks to this and I believe pausing the audio is not possible (haven't tested)
https://www.renpy.org/doc/html/audio.ht ... ight=audio
In addition to the normal channel, there is one special channel, audio. The audio channel supports playing back multiple audio files at one time, but does not support queueing sound or stopping playback.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

nanashine
Regular
Posts: 129
Joined: Thu Nov 30, 2017 1:44 pm
itch: renpytom
Contact:

Re: Play/Pause audio with textbutton?

#11 Post by nanashine »

Imperf3kt wrote: Mon Mar 11, 2019 2:33 pm If you want multiple songs playing at the same time, rather than registering one channel per each song, you could use the "audio" channel.

But there are drawbacks to this and I believe pausing the audio is not possible (haven't tested)
https://www.renpy.org/doc/html/audio.ht ... ight=audio
In addition to the normal channel, there is one special channel, audio. The audio channel supports playing back multiple audio files at one time, but does not support queueing sound or stopping playback.
Thank you so much! I'll give it a look :)
I can't access my other account cause I don't remember the e-mail I used *cries in emoji*.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]