How can you create an auto playlist with the HTML5 Audio tag?

▼魔方 西西 提交于 2019-12-05 09:34:32

问题


How can you create an auto playlist with the audio tag from HTML5?

What I'm trying to achieve is a player with only the play/pause button big 30x25 px, the player autoplays when the page loads and when the song ends it'll play the next song automatically. Trying to have 3-4 songs in a playlist.


回答1:


Check out this post: using jquery + <audio> w/ php/mysql to loop playback times

var sfx = new Audio('sfx.wav');
var sounds = a.addTextTrack('metadata');

// add sounds we care about
sounds.addCue(new TextTrackCue('dog bark', 12.783, 13.612, '', '', '', true));
sounds.addCue(new TextTrackCue('kitten mew', 13.612, 15.091, '', '', '', true));

function playSound(id) {
  sfx.currentTime = sounds.getCueById(id).startTime;
  sfx.play();
}

I just updated this with a better example of using cues as specified here: http://dev.w3.org/html5/spec/Overview.html




回答2:


I'm using similar code to the following on my site to play a continuous playlist of audio on my site:

<script type="text/javascript">
        var nextVideo = "/Link/To/Next/File.mp3";
        var audioPlayer = document.getElementById('audio_with_controls');
        audioPlayer.addEventListener('ended', PlayNext);

        function PlayNext() {
            audioPlayer.src = nextVideo;
            audioPlayer.play();
        }
</script>

It would be very easy for you to add an array of files that you could loop through (using arrays etc). You could also have the playlist displayed to the user, with each track being a clickable link that would call JS to change the player's "src" property.

Hope this helps!



来源:https://stackoverflow.com/questions/9636449/how-can-you-create-an-auto-playlist-with-the-html5-audio-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!