Announcement

Collapse
No announcement yet.

Example: Creating an on/off button to toggle background audio

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Example: Creating an on/off button to toggle background audio

    This will make a button on all pages of your project that is used to toggle the background audio between play/pause. All you need to do is add a button to the first page of your project and then set the script for it. When you found the perfrect lokation for it you just use ctrl+c to copy the button. Then go to each page of your projekt making sure no objekt on the page is selected and press ctrl+v and the button will be placed in the same place as on the page you coppied it from. Then all you need is to add the rest of the script to the page preload tab, this can also bedone by copy pasting. I have included the button I used so you also can use it.

    Here is the code. See an explanation at the bottom.

    -- CODE FOR ON PRELOAD ON ALL PAGES --

    strMusicP = "0"; -- This line should only be on the first page. Else you will reset the variable every time you change page.

    -- If strMusicP is 0 then the background audio is playing so the button state is set to up
    if strMusicP == "0" then
    Audio.Play(CHANNEL_BACKGROUND);
    Button.SetText("Lyd til fra", "2");
    Button.SetState("Lyd til fra", BTN_UP);
    -- If strMusicP is 1 that means that a user have pressed the button and the background audio is paused and the button state is set to down
    elseif strMusicP == "1" then
    Audio.Pause(CHANNEL_BACKGROUND);
    Button.SetText("Lyd til fra", "1");
    Button.SetState("Lyd til fra", BTN_DOWN);
    end

    -- CODE FOR THE BUTTON ON CLICK --

    -- The button text is from start set to 2 because the background audio is set to play on application run.
    strStateLyd = Button.GetText("Lyd til fra");

    -- If the text is 1 then the audio is paused so we change the audio state to play and change the text of the button to 2 for play mode. also the variable strMusicP is set to 0
    if strStateLyd == "1" then
    Audio.Play(CHANNEL_BACKGROUND);
    Button.SetText("Lyd til fra", "2");
    strMusicP = "0";

    -- This is the default setting of the button. if the text is 2 then the button have not been pressed untill now so we pause the music and change the text to 1 and set the variable strMusicP to 1 so that other pages will know the user have paused the music
    elseif strStateLyd == "2" then
    Audio.Pause(CHANNEL_BACKGROUND);
    Button.SetText("Lyd til fra", "1");
    strMusicP = "1";
    end
    The strMusicP Variable is on the first page preload set to 0. The reason the variable is set on the page preload and not on the button on the first page is in the case that a user does not click on it. because all other pages will start by looking for it. Also the script does not work if you by accident includes this line on all pages preload tab, because then you will reset the variable on page load so that every page starts out by thinking that the music is playing even though the user paused it.

    The Button.SetText 1 or 2 can be changed to fit your purpose. The reason I used 1 and 2 is that I set the text offset on the button to way out of range so the text is not visoble to the user, because the button I made for this hase an Icon on it that changes with the button State.
    Now you are proberbly wondering why then I use Button.GetText and not Button.GetState. The reason is simple. If you on the "On Click" use Button State in stead of Button Text as the trigger the script will not work because when you press the button it will always be down when the script is run.

    I've attached the button file to this thread so you can use it to. It is actually a screendump of the Play/Mute sound button from Microsoft Media Player.
    The text offset on this button is set way to the right of the button so that it is not visible to users. If you want text on the button you need to use the Button Maker to move the text back into the button area.
    (Tried to upload the button btn file but that file type is not permitted in this forum so it's zipped.)

    Kind Regards,
    Jonas DK
Working...
X