Recording Audio Listened | XM Community
Skip to main content

Hello,
This is a repost of an earlier question that I am still struggling with.
I am trying to create a survey in which I present an audio file for the participants to listen to. However, I would like to record how much of the total audio the participants actually choose to listen to (how much they choose to skip, etc.). While I am very new to Javascript/ jQuery, a friend helped me write code to record this information in an embedded data field called "totalTime." This code is placed in on the survey page where the audio file is. However, when I tried to enter this code onto the question, the embedded data (placed at the start of the survey) always reports a value of 0 despite the amount of audio is listened to. The console log is reporting numbers, followed by NaN. Thanks for any help!

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
function roundNumber(number, digits) {
        var multiple = Math.pow(10, digits);
        var rndedNum = Math.round(number * multiple) / multiple;
        return rndedNum;
    };


    // variables to keep track of progress
    var audioListened = 0;
    var audioSkipped = 0;
    var lastPosition = 0;


    // initialize data to 0
Qualtrics.SurveyEngine.setEmbeddedData('totalTime', 0);


    var audio_element = jQuery("audio.qmedia") // HTML audio element to track

    // as the audio plays, we update the data with the total amount of time listened
    audio_element.on('timeupdate',  function(event) {
        audioListened = event.currentTime - audioSkipped;
lastPosition = event.currentTime;
console.log(audioListened)
Qualtrics.SurveyEngine.setEmbeddedData('totalTime', audioListened )
})


// when participant skips time, we keep track of this to subtract from total time
    audio_element.on('seeked', function(event) {
        audioSkipped = (event.currentTime - lastPosition); 
console.log(audioSkipped)
});



});


Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/


});


Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/


});

So assuming you created a question, call it Q1, then added the audio in Q1 and put the JavaScript above in the Q1's JavaScript. Where are you trying to show the totalTime embedded data? If you try to show it on Q2, that's in the same block as Q1, then you will fail, the totalTime embedded data will either show in the next block or if you put a break between Q1 and Q2. Try that. If that's not your setting, please write down more detail on how you set this up or even leave a survey export here.


Leave a Reply