T.O.
T.O.

Reputation: 47

How to control which mp3 files to play using Java script on Qualtrics?

I am very new to coding a Java script and am trying to embed script lines that enable Qualtrics to play two types (i.e., High or Low) of sounds (mp3 files uploaded on the website) depending on the response of a participant.

Below Java script embedded in Qualtrics' survey enables me to detect the keyboard press for the answer instead of mouse click (i.e., clicking a bubble among multiple choice radio button which can be quite time consuming).

Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place Your Javascript Below This Line*/
    this.hideNextButton();
    this.hidePreviousButton();

    var that = this;

    Event.observe(document, 'keydown', function keydownCallback(e) {
      var choiceID = null;

      switch (e.keyCode) {
        case 72: // 'h' was pressed
          choiceID = 1;
          break;
        case 76: // 'l' was pressed
          choiceID = 2;
          break;
      }

      if (choiceID) {
        Event.stopObserving(document, 'keydown', keydownCallback);
        that.setChoiceValue(choiceID, true);
        that.clickNextButton();
      }
    });
});

I would very much appreciate it if someone could teach me possible lines that could be inserted below the clines of "choiceID = 1" or "choiceID = 2" to play the selected mp3 files (i.e., if choiceID==1 then play High.mp3; if choiceID==2 then play Low.mp3).

I use the below Qualtrics' source code to autoplay the mp3 files in the survey. So I think I need some Java script lines to execute the Qualtrics' source line like below.

 
<audio autoplay="" src="http://www.chrisasplund.com/AAB-SiD/High.mp3">&nbsp;</audio>

Thank you so much for your time and kindness!!!

Upvotes: 0

Views: 592

Answers (1)

T. Gibbons
T. Gibbons

Reputation: 5029

Add an id to your audio tags like:

<audio id="high" autoplay="" src="http://www.chrisasplund.com/AAB-SiD/High.mp3">&nbsp;</audio>

Then, to play it:

$('high').play();

Upvotes: 1

Related Questions