Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <title> html5 audio support for all browsers </title>
- <script type="text/javascript" src="audio-player/audio-player.js"></script>
- <script type='text/javascript'>//<![CDATA[
- if (!window.addEventListener) { // for compatibility with IE8 and below
- window.addEventListener = function (type, listener, useCapture) {
- attachEvent('on' + type, function () {
- listener(event);
- });
- };
- }
- window.addEventListener('load',function(){
- var i = 0; // song index
- //var songList = [ '01.Adresa Ja,Adresa Ty.mp3', '02.uzemie Zazrakov.mp3', '03.Koloseum.mp3' ];
- var songList = [
- 'songs/loop1.mp3',
- 'songs/loop2.mp3',
- 'songs/loop3.mp3',
- 'songs/loop4.mp3',
- 'songs/loop5.mp3',
- 'songs/loop6.mp3',
- 'songs/loop7.mp3',
- 'songs/loop8.mp3',
- 'songs/loop9.mp3'
- ];
- // create audio tag
- var audiocontainer = document.getElementById('audiocontainer'); //or document.body;
- var ae = document.createElement('audio');
- ae.id = 'audioplayer';
- ae.src = songList[0];
- audiocontainer.appendChild(ae);
- // create controls
- var btnPlay = document.createElement('button');
- var btnPrev = document.createElement('button');
- var btnNext = document.createElement('button');
- var testDiv = document.createElement('div');
- btnPlay.innerHTML = 'Play/Pause';
- btnPrev.innerHTML = 'Previous song';
- btnNext.innerHTML = 'Next song';
- testDiv.id = "test";
- audiocontainer.appendChild(btnPlay);
- audiocontainer.appendChild(btnPrev);
- audiocontainer.appendChild(btnNext);
- audiocontainer.appendChild(testDiv);
- //add event handlers
- ae.onplay = function () {
- document.getElementById('test').innerHTML = 'Now playing #' + (i+1) + ' ' + songList[i];
- };
- ae.onended = function(){
- NextSong();
- };
- btnPlay.onclick = function() {
- if (ae.paused) ae.play();
- else ae.pause();
- };
- btnPrev.onclick = PrevSong;
- btnNext.onclick = NextSong;
- ae.play();
- function PrevSong() {
- i = (i > 0)? i - 1 : songList.length - 1; // choose previous index
- ae.setAttribute("src", songList[i]);
- ae.play();
- }
- function NextSong() {
- i = (i < songList.length - 1)? i + 1 : 0; // choose next index
- ae.setAttribute("src", songList[i]);
- ae.play();
- }
- // flash fallback for IE8 and others old browsers without audio tag or mp3 playback support.
- // Download the plugin here: http://wpaudioplayer.com/standalone/
- // Additional info here:
- // http://matt.coneybeare.me/getting-html5-audio-tag-and-flash-fallback-to/
- // http://stackoverflow.com/questions/11681856/html5-audio-tag-flash-fall-back/
- // http://stackoverflow.com/questions/17387702/html5-audio-song-only-playing-first-2-in-array
- AudioPlayer.setup("audio-player/player.swf", { width: 290 });
- if (!(!!(ae.canPlayType) && ("no" !== ae.canPlayType("audio/mpeg")) && ("" !== ae.canPlayType("audio/mpeg")))) {
- AudioPlayer.embed("audioplayer", {
- soundFile: songList.join(','),
- loop: "yes",
- autostart: "yes",
- width:0
- });
- }
- }, false);
- //]]>
- </script>
- </head>
- <body>
- <div id="audiocontainer"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement