Wednesday, 29 October 2014

create audio player using jquey, html5 and css3- part 1

Step 1 – Downloading MediaElement.js

First we need to download the “MediaElement.js” script and extract it. Then from the “build” folder we need three files:
  • flashmediaelement.swf
  • mediaelement-and-player.min.js
  • silverlightmediaelement.xap
Then copy all these three files to the same directory, I will copy for my “js”folder.

Step 2 – HTML Markup

Now, we need to link to the jQuery Library, we can host it locally or use the one hosted by Google. Then we need to link to “mediaelement-and-player.min.js” script file and the CSS file. All this three files need to be inside of the <head> tag.
<head>
<title>Audio Player</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>   
 <script src="js/mediaelement-and-player.min.js">
</script>   
 <link rel="stylesheet" href="css/style.css" media="screen">
</head>
To create the player we will add a <div> width the class “audio-player”. This div will be the container for our player elements. Let’s add a <h1> tag for the song title and <img> for the cover. Then we will add the <audio> tag that will link to our song and we’ll also set the id to “audio-player”.
<div class="audio-player">
    <h1>Demo - Preview Song</h1>
    <img class="cover" src="img/cover.png" alt="">
    <audio id="audio-player" src="demo.mp3" type="audio/mp3" controls=
"controls"></audio>
</div>


To finish we need to add this code before the ending of the </body> tag. Also we need to add the same id as we used in the <audio> tag to load the player. You can set some settings too, for more info read the “MediaElement.js”documentation.

<script>
    $(document).ready(function() {
        $('#audio-player').mediaelementplayer({
            alwaysShowControls: true,
            features: ['playpause','volume','progress'],
            audioVolume: 'horizontal',
            audioWidth: 400,
            audioHeight: 120
        });
    });
</script>




Step 3 – Container Styles

First let’s add some reset styles for all the elements that we will use in the container.
1
2
3
4
5
6
7
8
9
10
11
12
.audio-player,
.audio-player div,
.audio-player h1,
.audio-player a,
.audio-player img,
.audio-player span,
.audio-player button {
    margin0;
    padding0;
    bordernone;
    outlinenone;
}
Now let’s style the player container, we will set the width to 400px and height to 120px. We will also add a css3 background gradient and rounded corners.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
div.audio-player {
    positionrelative;
    width400px;
    height120px;
 
    background#4c4e5a;
    background: -webkit-linear-gradient(top#4c4e5a 0%#2c2d33 100%);
    background: -moz-linear-gradient(top#4c4e5a 0%#2c2d33 100%);
    background: -o-linear-gradient(top#4c4e5a 0%#2c2d33 100%);
    background: -ms-linear-gradient(top#4c4e5a 0%#2c2d33 100%);
    background: linear-gradient(top#4c4e5a 0%#2c2d33 100%);
 
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}

Step 4 – Title & Cover

Let’s position the title and cover on the player container and then add some typography styles for the title.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.audio-player h1 {
    positionabsolute;
    top37px;
    left165px;
 
    font-familyHelveticaArialsans-serif;
    font-weightbold;
    font-size14px;
    color#ececec;
    text-shadow1px 1px 1px rgba(0,0,0, .5);
}
 
.audio-player .cover {
    positionabsolute;
    top0;
    left0;
}

No comments:

Post a Comment