HTML5 Audio player with playlist
HTML5 audio player. Many of you faced with the task of creating audio
player at least once in your life. Quite often , you simply choose one
of the available players, often it was the flash player. However, you
may have already noticed, that these flash players do not work properly
on mobile devices (iPhone / Android). Today I am going to tell you about
how to create your own audio player (using HTML5 technology). This
player consists of next elements: title, author, cover, basic controls
(play / pause, rewind / forward) , two sliders (jQuery UI sliders):
volume slider and and a song tracker, and even more: we will also have a
playlist with a list of available songs.
I
believe that you already know how to create a simple audio player using
a standard <audio> element. In our example, we will not use a
special markup for this elemet, we will create our own player with all
the necessary controls. We will control the player using the event
handlers of a created (in JavaScript) audio element.
Step 1. HTML
As usual, we have to include several files in the head section:
04 | < meta charset = "utf-8" /> |
05 | < meta name = "author" content = "Script Tutorials" /> |
06 | < meta name = "viewport" content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> |
07 | < title >HTML5 Audio player with playlist | Script Tutorials</ title > |
10 | < link href = "css/styles.css" rel = "stylesheet" type = "text/css" /> |
11 | < script type = "text/javascript" src = "js/jquery-1.7.2.min.js" ></ script > |
12 | < script type = "text/javascript" src = "js/jquery-ui-1.8.21.custom.min.js" ></ script > |
13 | < script type = "text/javascript" src = "js/main.js" ></ script > |
And now, please have a look at our player’s markup:
02 | < div class = "pl" ></ div > |
03 | < div class = "title" ></ div > |
04 | < div class = "artist" ></ div > |
05 | < div class = "cover" ></ div > |
06 | < div class = "controls" > |
07 | < div class = "play" ></ div > |
08 | < div class = "pause" ></ div > |
09 | < div class = "rew" ></ div > |
10 | < div class = "fwd" ></ div > |
12 | < div class = "volume" ></ div > |
13 | < div class = "tracker" ></ div > |
15 | < ul class = "playlist hidden" > |
16 | < li audiourl = "01.mp3" cover = "cover1.jpg" artist = "Artist 1" >01.mp3</ li > |
17 | < li audiourl = "02.mp3" cover = "cover2.jpg" artist = "Artist 2" >02.mp3</ li > |
18 | < li audiourl = "03.mp3" cover = "cover3.jpg" artist = "Artist 3" >03.mp3</ li > |
19 | < li audiourl = "04.mp3" cover = "cover4.jpg" artist = "Artist 4" >04.mp3</ li > |
20 | < li audiourl = "05.mp3" cover = "cover5.jpg" artist = "Artist 5" >05.mp3</ li > |
21 | < li audiourl = "06.mp3" cover = "cover6.jpg" artist = "Artist 6" >06.mp3</ li > |
22 | < li audiourl = "07.mp3" cover = "cover7.jpg" artist = "Artist 7" >07.mp3</ li > |
Looks easy, does not it? As you can see – all the necessary elements are included here.
Step 2. CSS
The
time has come to turn our bare HTML model into a beautiful player, for
that, we need to define the styles used for each element.
css/styles.css
006 | background : transparent url ( "../images/spr.png" ) no-repeat scroll center top ; |
013 | font-family : verdana ; |
017 | -moz-user-select: none ; |
018 | -webkit-user-select: none ; |
019 | -ms-user-select: none ; |
033 | background : transparent url ( "../images/spr.png" ) no-repeat scroll -274px -175px ; |
045 | background : transparent url (../data/cover 1 .jpg) no-repeat scroll center top ; |
046 | border-radius: 5px 5px 5px 5px ; |
061 | .controls .play, .controls .pause, .controls .rew, .controls .fwd { |
062 | background : transparent url ( "../images/spr.png" ) no-repeat scroll 0 0 ; |
068 | background-position : -8px -171px ; |
071 | background-position : -8px -198px ; |
075 | background-position : -54px -171px ; |
078 | background-position : -100px -171px ; |
080 | .controls .play:hover { |
081 | background-position : -8px -170px ; |
083 | .controls . pause :hover { |
084 | background-position : -8px -197px ; |
086 | .controls .rew:hover { |
087 | background-position : -54px -170px ; |
089 | .controls .fwd:hover { |
090 | background-position : -100px -170px ; |
113 | background : transparent url ( "../images/spr.png" ) no-repeat scroll 5px -222px ; |
127 | .volume .ui-slider-handle { |
128 | background : url ( "../images/spr.png" ) no-repeat scroll -201px -188px rgba( 0 , 0 , 0 , 0 ); |
133 | background-color : #333333 ; |
134 | border-radius: 5px 5px 5px 5px ; |
135 | list-style-type : none ; |
136 | margin : -10px 0 0 2px ; |
137 | padding-bottom : 10px ; |
146 | margin : 0 0 5px 15px ; |
Step 3. JavaScript
The
beautiful player that does nothing – useless. To make it a true work of
art, you have to fill it with events and functions. First at all, we
need to initialize several variables:
js/main.js
03 | var tracker = $( '.tracker' ); |
04 | var volume = $( '.volume' ); |
07 | initAudio($( '.playlist li:first-child' )); |
18 | start: function (event,ui) {}, |
19 | slide: function (event, ui) { |
20 | song.volume = ui.value / 100; |
22 | stop: function (event,ui) {}, |
29 | start: function (event,ui) {}, |
30 | slide: function (event, ui) { |
31 | song.currentTime = ui.value; |
33 | stop: function (event,ui) {} |
Then, I prepared several general functions to handle with audio:
01 | function initAudio(elem) { |
02 | var url = elem.attr( 'audiourl' ); |
03 | var title = elem.text(); |
04 | var cover = elem.attr( 'cover' ); |
05 | var artist = elem.attr( 'artist' ); |
07 | $( '.player .title' ).text(title); |
08 | $( '.player .artist' ).text(artist); |
09 | $( '.player .cover' ).css( 'background-image' , 'url(data/' + cover+ ')' );; |
11 | song = new Audio( 'data/' + url); |
14 | song.addEventListener( 'timeupdate' , function (){ |
15 | var curtime = parseInt(song.currentTime, 10); |
16 | tracker.slider( 'value' , curtime); |
19 | $( '.playlist li' ).removeClass( 'active' ); |
20 | elem.addClass( 'active' ); |
25 | tracker.slider( "option" , "max" , song.duration); |
27 | $( '.play' ).addClass( 'hidden' ); |
28 | $( '.pause' ).addClass( 'visible' ); |
33 | $( '.play' ).removeClass( 'hidden' ); |
34 | $( '.pause' ).removeClass( 'visible' ); |
And then I started to add event handlers to our control buttons. Play / Pause buttons:
02 | $( '.play' ).click( function (e) { |
09 | $( '.pause' ).click( function (e) { |
In
order to turn to another song in the playlist, we have to stop playing a
current song, pick a next (or previous) object in the playlist, and
re-initialize our Audio element. Forward / Rewind buttons:
02 | $( '.fwd' ).click( function (e) { |
07 | var next = $( '.playlist li.active' ).next(); |
08 | if (next.length == 0) { |
09 | next = $( '.playlist li:first-child' ); |
15 | $( '.rew' ).click( function (e) { |
20 | var prev = $( '.playlist li.active' ).prev(); |
21 | if (prev.length == 0) { |
22 | prev = $( '.playlist li:last-child' ); |
Finally, few functions to handle with the playlist:
02 | $( '.pl' ).click( function (e) { |
05 | $( '.playlist' ).fadeIn(300); |
09 | $( '.playlist li' ).click( function () { |
Step 4. Images
All used images are packed into one single sprite file: nav.png