Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

basic Javascript - how to reference on online audio file

Explorer ,
Oct 01, 2016 Oct 01, 2016

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.

778
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Oct 01, 2016 Oct 01, 2016

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.

Translate
Explorer ,
Oct 01, 2016 Oct 01, 2016

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 01, 2016 Oct 01, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Oct 01, 2016 Oct 01, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 01, 2016 Oct 01, 2016

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Oct 01, 2016 Oct 01, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 01, 2016 Oct 01, 2016

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2016 Oct 01, 2016

You only want JS? I would use advanced actions but you don't like to use them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 01, 2016 Oct 01, 2016

The problem for this activity is that I'm feeding text and audio from my google sheet and google drive.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Oct 02, 2016 Oct 02, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 02, 2016 Oct 02, 2016

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..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Oct 02, 2016 Oct 02, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 02, 2016 Oct 02, 2016
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources