How to randomize music across blocks using embedded data | XM Community
Skip to main content

I have four conditions that need to be randomized. Each condition has been built in four blocks, which are placed under a randomizer in the Survey Flow.

Within each block, there's a question asking participants to listen to music for 7 minutes. After the 7 minutes, participants will rate some scales. I have 13 music excerpts, and I want them to be randomized during the 7-minute running but not to be repeated across conditions or blocks.

I've written a JavaScript code and created an embedded data variable named “musicList” in the Survey Flow. When I preview the survey, the first displayed block successfully played three different excerpts. However, the next three blocks each played the same excerpt and repeated until the 7 minutes ended.

Can anyone help me fix it, or is there another way to solve this problem? Thanks in advance!

Below is the javascript:

 

Qualtrics.SurveyEngine.addOnload(function() {
var musicTracks = c(URLs of the music excerpts];

function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
}
return array;
}

var musicOrder = shuffleArray(musicTracks);
var audioElement = document.getElementById("music-player");
var currentMusicIndex = 0;


// Function to play the next music track
function playNextMusicTrack() {
    if (currentMusicIndex < musicOrder.length) {
        var nextTrack = musicOrderkcurrentMusicIndex]; //if there are more tracks to play, then retrieves the URL of the next track from the "musicOrder" array

        // Check if the track has been played before
        if (!playedMusic.includes(nextTrack)) { // if the track hasn't been played, then proceeds to play it
            audioElement.src = nextTrack;
            audioElement.play();
            playedMusic.push(nextTrack);
        }

    } else {
    }
      currentMusicIndex++; // increment the currentMusicIndex
}

// Retrieve the playedMusic array from embedded data or initialize it
var playedMusic = "${e://Field/musicList}";
if (!playedMusic) {
    playedMusic = }];
}

// Event listener to detect when a track finishes playing
audioElement.addEventListener("ended", function () {
    playNextMusicTrack(); // Play the next track when the current one ends
});

playNextMusicTrack(); // Start playing the first track


// Update embedded data with the played music
    Qualtrics.SurveyEngine.setEmbeddedData("PlayedMusic", playedMusic);
});

@skyeeeeeee 

Your playedMusic get data from musicList BUT update it back to PlayedMusic so it’s a reset eachtime the code run.

Also move setEmbeddedData to the inside of the function to updated playedMusic array after each new excerpt is played right after playedMusic.push(nextTrack);

The rest of the code looks fine. You should throw in some console log to track and make sure that the value got pulled in is the right & updated one.
Let me know if this helps


Leave a Reply