Page 1 of 1

[solved] How can I make it play a random sound?

Posted: Tue Oct 09, 2018 5:20 pm
by Nanahs
So, I'm using hotspot. There's a buttom that when you click, you play a sound (not music).
But I wanted to have more than one sound, that would play randomly when the person clicks.

I was trying to use:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound","jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3", loop=True, shuffle=True)
But it didn't work. Only the first sound (jhoneffect.mp3) plays when I click the buttom.
I can't change "sound" to "music", because that would conflit with the background music in the scene. I need it to be a "sound channel".

ps: this label is sumulating a radio. I have a buttom to "play/stop" music. And another one to "play/stop" sound.
That's why I have to put them separately. Because I want to be able to stop the "sound" without stopping "music".

Does anyone know where the mistake is in the code?

Thanks :)

Re: How can I make it play a random sound?

Posted: Tue Oct 09, 2018 5:56 pm
by Imperf3kt
You can use renpy.random to select a file from a list.
See my cookbook entry for an example.
viewtopic.php?f=51&t=42259

Re: How can I make it play a random sound?

Posted: Tue Oct 09, 2018 6:28 pm
by Nanahs
Imperf3kt wrote: Tue Oct 09, 2018 5:56 pm You can use renpy.random to select a file from a list.
See my cookbook entry for an example.
viewtopic.php?f=51&t=42259
Thank you! I think I understood the process :)
The only thing I'm still in doubt is, after making those changes, how would I use this in a hotspot action?

Re: How can I make it play a random sound?

Posted: Tue Oct 09, 2018 9:34 pm
by Imperf3kt
An "action" will work the same for a Hotspot as it would for an imagebutton, textbutton or any other button.

I can't go over it right now, but just use it as shown in the example, replacing the appropriate buttons with your Hotspot.

Re: How can I make it play a random sound?

Posted: Tue Oct 09, 2018 10:51 pm
by Nanahs
Imperf3kt wrote: Tue Oct 09, 2018 9:34 pm An "action" will work the same for a Hotspot as it would for an imagebutton, textbutton or any other button.

I can't go over it right now, but just use it as shown in the example, replacing the appropriate buttons with your Hotspot.
Oh, ok! Thank you! :)

Re: How can I make it play a random sound?

Posted: Wed Oct 10, 2018 1:38 pm
by IrinaLazareva
Nanahs wrote: Tue Oct 09, 2018 5:20 pm So, I'm using hotspot. There's a buttom that when you click, you play a sound (not music).
But I wanted to have more than one sound, that would play randomly when the person clicks.
I was trying to use:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound","jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3", loop=True, shuffle=True)
Try:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]))
Or somewhere outside of main code:

Code: Select all

define mysound = ["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]
and then:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(mysound))
About random -> https://renpy.org/doc/html/other.html#renpy-random
Nanahs wrote: Tue Oct 09, 2018 5:20 pm I can't change "sound" to "music", because that would conflit with the background music in the scene. I need it to be a "sound channel".
You can create a new channel - > https://renpy.org/doc/html/audio.html#r ... er_channel

Re: How can I make it play a random sound?

Posted: Wed Oct 10, 2018 1:48 pm
by IrinaLazareva
And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound

Re: How can I make it play a random sound?

Posted: Wed Oct 10, 2018 2:21 pm
by Nanahs
IrinaLazareva wrote: Wed Oct 10, 2018 1:48 pm And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
The first option worked! Thank you so much! :)

Re: How can I make it play a random sound?

Posted: Wed Oct 10, 2018 9:59 pm
by Nanahs
IrinaLazareva wrote: Wed Oct 10, 2018 1:48 pm And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
I used the first code you suggested. "loop=True" is not working to make the sound loop. Do you know why? :?:

ps: oh, never mind. It's working now hah

Re: How can I make it play a random sound?

Posted: Thu Oct 11, 2018 12:38 pm
by Nanahs
IrinaLazareva wrote: Wed Oct 10, 2018 1:48 pm And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
Hello! Sorry to "call" you again hah

I used:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]),loop=True)
But now, how should I make this sound stop when jumping to the next label? I keeps playing.
I tried a few codes I know, but they didn't work. Thanks.

Re: [solved] How can I make it play a random sound?

Posted: Thu Oct 11, 2018 2:34 pm
by Imperf3kt
One way is to stop the sound channel

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]),loop=True)


label start:
    "I assume you start here."

label silence:
    stop sound
    "Ahh, nice and quiet."

Re: [solved] How can I make it play a random sound?

Posted: Thu Oct 11, 2018 4:44 pm
by Nanahs
Imperf3kt wrote: Thu Oct 11, 2018 2:34 pm One way is to stop the sound channel

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]),loop=True)


label start:
    "I assume you start here."

label silence:
    stop sound
    "Ahh, nice and quiet."
Hello! For some reason "stop sound" was not working, but it worked now. Thank you! :)