Play sound/ stop sound when button clicked. (mute button)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Play sound/ stop sound when button clicked. (mute button)

Post by sinep » Sat Jun 20, 2015 10:15 am

I have a sound button on my main card and, if it's hilited/ selected, the sound will play on each additional card as well as the main card. I'm trying to play a different sound file for each card because they're themed differently.

I'm trying to create a a mute button so that it starts and stops the sound when clicked.

I know that a simple on mouseup would normally work but because it opens as playing if the sound was selected on the main card and switches to a different song when different cards are opened, it's messing it up somewhere.

This is the code that's not working on my card that has a "mute" button" ("Main" and "Peace" are different cards):

on openCard
if the hilite of btn "Sound" of card "Main" then
play audioClip "jump.wav" looping
else
play stop
end if

If the sound is playing when the card is opened it will mute the sound if I click the intended box. However, it will not start playing the sound again once I click the box again.

end openCard

on mouseUp

if the hilite of btn "Sound" of card "Peace" then
play audioClip "jump.wav" looping
else
play stop
end if

end mouseUp

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Play sound/ stop sound when button clicked. (mute button

Post by zaxos » Sat Jun 20, 2015 5:29 pm

Have you tryed:
Play audioclip "jump.wav" of card "peace" of stack "??"
Knowledge is meant to be shared.

sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Re: Play sound/ stop sound when button clicked. (mute button

Post by sinep » Sun Jun 21, 2015 2:43 am

I tried adding that but it didn't seem to change it. I think it might have to do with the order of how I have it written in the script. I'm really new at this so even the simplest of things throw me off sometimes.

When my new card opens the background sound for that card starts playing like it should, I just can't get it to mute/unmute when I want. It mutes but doesn't unmute when I click the "mute" button. I'll keep at it and hopefully have a better update :)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Play sound/ stop sound when button clicked. (mute button

Post by Klaus » Sun Jun 21, 2015 3:30 pm

Hi sinep,

not sure I get this, you are referring to 2 different buttons in your "opencard" and "mouseup" scripts!
...
on openCard
if the hilite of btn "Sound" of card "Main" then
...
on mouseUp
if the hilite of btn "Sound" of card "Peace" then
...
Are you sure they are "in sync"? Know what I mean?


Best

Klaus

P.S.
Imported audio- and videoclips are "global" and do not belong to any specific object or card!

sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Re: Play sound/ stop sound when button clicked. (mute button

Post by sinep » Mon Jun 22, 2015 9:01 pm

Klaus wrote:Hi sinep,

not sure I get this, you are referring to 2 different buttons in your "opencard" and "mouseup" scripts!
...
on openCard
if the hilite of btn "Sound" of card "Main" then
...
on mouseUp
if the hilite of btn "Sound" of card "Peace" then
...
Are you sure they are "in sync"? Know what I mean?


Best

Klaus

P.S.
Imported audio- and videoclips are "global" and do not belong to any specific object or card!

Yes, they are two different buttons. One is on my main card/front screen and the other(s) are on their respective cards. I wanted it so that if someone selected "sound" on the main card it would play sound through each subsequent card unless the "mute" button on that card was clicked. Likewise, if the "sound" was unselected on the main card, it would not play sound each time a new card was opened.

I'm working on the global now. This is really like learning a second language for me so it's just a bit confusing. As well, I think I might just take out the "sound" button on the main card and allow each card to have their own under the "global."

Hopefully this post can help others in the future. I've been completely unable to find info or guides on how to do it. I thought mute buttons were common but alas.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Play sound/ stop sound when button clicked. (mute button

Post by jacque » Tue Jun 23, 2015 7:42 pm

There's an easier way to do what you want. But first, when Klaus said that sounds are "global" he wasn't referring to global variables, he just meant that imported sounds are not controls, they are just resources added to the stack, and as such, they have a global scope and don't belong to any particular card or background, they exist everywhere. That's why he put "global" in quotes, but I can see how that would be confusing to a newcomer. You can ignore global variables for now, you don't need them. You can also stop referring to the audioclip as though it were placed on any particular card, because it is its own boss and belongs to nothing else.

The best way to do what you want is to set up your button as a shared background group. It's okay to have a group with only a single object in it. Select the "Sound" button on the first card and group it. Delete all the other copies of the button on all cards but the first one. Then use the Objects menu to place that group on every card. Now you have a single button that appears on every card but is actually only a single object that is shared.

To get this single button to play the correct audioclip on different cards, you need to store the name of the clip somewhere on the card. The most common method is to create a custom property on each card that holds the name of the correct clip. Open the property inspector for one of the cards, go to the custom properties pane, and create a custom property. I'll call it "cAudio". In the lower pane, type in the name of the audioclip that should play on this particular card. Repeat this process for each card that plays audio. Each card needs to have a cAudio property, but the content of the property will vary for each card.

Now put a script into the shared button that toggles the audio on and off:

Code: Select all

on mouseUp
  set the hilite of me to not the hilite of me -- toggles the hilite
  if the hilite of me then -- it's on
    play audioclip (the cAudio of this card) looping
  else -- it's off
   play stop
  end if
end mouseUp
Finally, if you want the audio to start automatically when the stack opens, you can just send a "mouseup" command to the button:

Code: Select all

send "mouseup" to button "sound"
This will only work if the button's hilite is off to begin with. I'll leave it to you to figure out how to get around that.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply