Copy link to clipboard
Copied
Could someone help me with an example of how to play an online audio? I already have a slide button - I just want to play on-click an audio file like this one: http://jplayer.org/audio/ogg/Miaow-07-Bubble.ogg
Thank you.
1 Correct answer
just use:
var audio = new Audio(cloze61);audio.play();
You don't need any quotes around your variable. Also, if this is html5 in the script window you should use window.cloze61, your variable is most likely undefined.
also the "reply" at the end will break the code.
Copy link to clipboard
Copied
I found this works - but is there a better way?
var player = document.createElement('audio');
player.src = 'http://jplayer.org/audio/ogg/Miaow-07-Bubble.ogg';
player.load();
player.play();
Copy link to clipboard
Copied
And if my variable is a URL, why is it that this doesn't work?
var player = document.createElement('audio');
player.src = "'" + cloze61 + "'";
player.load()
player.play(); reply
Copy link to clipboard
Copied
just use:
var audio = new Audio(cloze61);audio.play();
You don't need any quotes around your variable. Also, if this is html5 in the script window you should use window.cloze61, your variable is most likely undefined.
also the "reply" at the end will break the code.
Copy link to clipboard
Copied
Thanks! This works well. I really like the simple answers.
This is html5, and I'm not using windows.cloze61 - Do you mean to put that at the top of the script? I have a previous script calling the variable in from a google sheet, so does that solve the problem?
Copy link to clipboard
Copied
You need window if it is a Captivate user variable. Your variable was already a string, so in effect you surrounded your irk with quotes.
Copy link to clipboard
Copied
Okay. One other question to you or anyone else who cares to answer. What then is the best way to stop and rewind the audio with each button click? I don't want the audio overlapping.
Copy link to clipboard
Copied
You only want JS? I would use advanced actions but you don't like to use them.
Copy link to clipboard
Copied
The problem for this activity is that I'm feeding text and audio from my google sheet and google drive.
Copy link to clipboard
Copied
Change the script to this:
var audio = new Audio(cloze61);
audio.currentTime = 0;audio.play();
Every time you click the button it will play from the beginning.
You really should separate the call to populate the colze61 variable and the action to play the audio.
Copy link to clipboard
Copied
Hi, I tried this. but the previous audio still plays over the new one.
I'm populating the variable "cloze61" with a button click on the 1st slide, where the user chooses which version of the game to play - and am using only these 3 lines of code on an audio player button on slide 2..
Copy link to clipboard
Copied
Hard to tell without seeing you total setup, but if you are using multiple audio files you'll need to determine if it is playing and if so, rewind and stop. There is no isPlaying variable so you'll need to check the currentTime against the duration. or use the same audio object.
You can set the audio object to null and then re-create it with a new source.
Copy link to clipboard
Copied
Thanks. I'm using a Google app script, in fact based on the "leaderboard" script from E-Learning brothers. There's no prob;em there as far as I can see. When a user chooses a number on slide 1, this is fed to the Google sheet, which then uses the number to pull up a column of values.. The Captivate project then imports these values and assigns them to a series of variables. One of the values is a URL pointing to an audio file on my Google drive. The audio player button only has one audio file to play, and that works fine, thanks to your help.
So I think the audio is playing from the web - outside of Captivate- if that makes sense.
I just want the user to be able to re-click the audio and start it over (or at least mute the first instance) before it finishes. I might even be able to combine this with a counter and advanced actions if that works, but I need a way to stop the audio from playing.

