Stop music on a slide after setting audio to play in all slides using Java

Dec 21, 2017

Hello! 

I set the audio of my course to play without restarting using some code I found on threats on this forum; now, I would like to stop the music in only one particular slide because there is a video. Is there anyway to do this? 

 

This is the code im using: 

//load the scripts dynamically into the head of the document
function add_line() {
var line = document.createElement("audio");
var head=document.getElementsByTagName('body')[0];
line.type = "audio/mp3";
line.src="https://www.bipedu.com/repository/music/people_management_music.mp3";
line.id="bgSong" ;
line.autoplay = true;
line.loop = true;
head.appendChild(line);
}

//but we only want to add these once!
if(document.getElementById('bgSong')==null){
add_line();
var audio = document.getElementById('bgSong');
audio.volume = 0.1;
}

 

 

Thank you very much! 

2 Replies
Anderson Rumuy

I got this Javascript from Owen Holt Post

Try this, You can adjust the volume of the backsound music

using Dial + JS to get the volume control of the backsound music

var player = GetPlayer();
var level = player.GetVar("Volume")
var audio = document.getElementById('bgSong');
audio.volume = level;

the Variable is "Volume" number.

 

Benjamin Weil

Dear Anderson,

I tried that hack in my storyline project together with your latest code

var audio = document.getElementById('bgSong');
audio.src="bensound-energy.mp3";
audio.load();
audio.play();
audio.volume = 0.4;

var player = GetPlayer();
var level = player.GetVar("Volume")
var audio = document.getElementById('bgSong');
audio.volume = level;

Of course I put that in my story.html <body> : <audio src="bensound-energy.mp3" id="bgSong" preload="auto" autoplay loop></audio>

But it didn't work (the music is played but I can't mute or control the level with the volume control). Would you have any idea why ?

Thank you !

Benjamin