Search code examples
javascripthtmlaudioplaybacksentence

Play consecutive sounds to construct an audio sentence with HTML5 and javascript


I'm trying to put together an audio map for a phonegap app. I want to construct simple sentences to create directions.

To narrow things down, I have an array of audio elements that I want to play consecutively to perform audio navigation. For example:

// playList[0].play --> "You are coming from the"
// playList[1].play --> "Reptile House"
// playList[2].play --> "(half-second pause as sentence period...)"
// playlist[3].play --> "Please select a destination from the list on the left."

At the bottom of this post is simplified code I've been playing with but not having much success with. I keep getting errors. Before suggesting using jplayer or another system, it seems to me that these are more geared towards playing songs with the ability to stop them. I don't need a full-blown jplayer-or-other-style interface. I just need people to press buttons so they can be guided through an audio map system. I keep getting this error in the console:

Uncaught TypeError: Object #selectionFrom has no method 'play' audio-map.js:142
(anonymous function) audio-map.js:142
jQuery.event.dispatch jquery-1.9.1.js:3074
jQuery.event.add.elemData.handle jquery-1.9.1.js:2750

I've tried the array as both element ID's and just variables.

var playList= ["#selectionFrom","#playReptileHouse","#play500msPause","#selectDestination"];
var playList= ["selectionFrom","playReptileHouse","play500msPause","selectDestination"];

I'd be greatful for any assistance. Here's the simplified script:

$(document).ready(function() {

    var playZooEntrance = document.createElement('audio');
    playZooEntrance.setAttribute('src', 'assets/audio/playZooEntrance.mp3');
    playZooEntrance.setAttribute('id', 'playZooEntrance');
    document.body.appendChild(playZooEntrance);

    var playReptileHouse = document.createElement('audio');
    playReptileHouse.setAttribute('src', 'assets/audio/playReptileHouse.mp3');
    playReptileHouse.setAttribute('id', 'playReptileHouse');
    document.body.appendChild(playReptileHouse);

    var selectionFrom = document.createElement('audio');
    selectionFrom.setAttribute('src', 'assets/audio/selectionFrom.mp3');
    selectionFrom.setAttribute('id', 'selectionFrom');
    document.body.appendChild(selectionFrom);

    var selectDestination = document.createElement('audio');
    selectDestination.setAttribute('src', 'assets/audio/selectDestination.mp3');
    selectDestination.setAttribute('id', 'selectDestination');
    document.body.appendChild(selectDestination);

    var play500msPause = document.createElement('audio');
    play500msPause.setAttribute('src', 'assets/audio/play500msPause.mp3');
    play500msPause.setAttribute('id', 'play500msPause');
    document.body.appendChild(play500msPause);

    $('#selected_from').click(function() {  // On the press of a button...
        var playList= ["selectionFrom","playReptileHouse","play500msPause","selectDestination"];
        var i;
        for (i = 0; i < playList.length; ++i) {
            console.log(playList[i]);
            playList[i].play();
            playList[i].setAttribute('ended',function(){/* next i??? */});
        }
    });
    // Play this consecutively...
    // playList[0].play --> "You're coming from the"
    // playList[1].play --> "Reptile House"
    // playList[2].play --> "(half-second pause)"
    // playlist[3].play --> "Please select a destination from the list on the left."
});

Solution

  • It seems you've made a type mistake : you ask a string ('SelectionFrom') to play when you mean to get the element having that id.
    So you might pre-store the elements in an array, or an object, while creating them, or find them by id using jquery ($('#'+playList[i]).