How to Create a cool Audio Player Plug-in via text tutorial _ HTML5 tutorial skills-

Source: Internet
Author: User
This tutorial mainly aims to demonstrate the use of the newly added Html5 tags and the DomAPI of the audio elements. The tutorial is very detailed and contains the sample code. If you need it, refer to it.

This is the UI of the audio player, which also hides the playing list of a song. The UI of the entire player, in addition to the portrait image of the big background and the star rating of the song, uses the image. All others are drawn by CSS and font-face, it seems that it is difficult to create only CD and dashboard. When playing a song, both CD and dashboard have an animation interaction effect. This will be explained in the following sections. Click the player CD to open the playlist ~

This phase of the tutorial mainly aims to display the newly added tags of Html5 and the use of Dom APIs of audio elements. Therefore, the rendering of the player UI is not described in detail here, if you have any questions, you can view the comments of the Case CSS file or leave a message below.

Main functions:

I. Playback, pause, last, next, volume increase/decrease

2. Click CD to open and close the playlist.

3. You can drag a local music file to the player for playback.

Html Structure


The Code is as follows:
























A




B







    E
    C
    D



    0: 00



    0: 00




    Audio tag

    In the above structure, we can find that the newly added audio tag of html5 has a controls attribute. As the name suggests, it is the controller of the player. The controls attribute specifies that the browser provides a playback control for the audio, for example, if this attribute is set in the audio tag in chrome, the player will appear in the following format. If this attribute is not set, it will be blank.


    The Code is as follows:

    However, due to the different effects of rendering the audio tag in different browsers, this simple method is not suitable for cross-browser use, in addition, the default player control of the browser provides too few functions .. Therefore, we usually hide the default playback control of the browser by not setting this attribute, and manually add additional labels and styles to customize the player UI.

    Playback Control

    After the player UI is drawn, we need to add corresponding event listening for the three main control buttons: playback, last and next.


    The Code is as follows:


    Var myAudio = $ ("# myAudio audio") [0];
    Var $ sourceList = $ ("# myAudio source ");
    Var currentSrcIndex = 0;
    Var currentSr = "";


    The Code is as follows:


    $ (". Btn_play"). click (function (){
    If (myAudio. paused ){
    MyAudio. play ();
    } Else {
    MyAudio. pause ();
    }
    });
    $ (". Btn_next"). click (function (){
    ++ CurrentSrcIndex> $ sourceList. length-1 & (currentSrcIndex = 0 );
    CurrentSrc = $ ("# myAudio source"). eq (currentSrcIndex). prop ("src ");
    MyAudio. src = currentSrc;
    MyAudio. play ();
    });
    $ (". Btn_previous"). click (function (){
    -- CurrentSrcIndex <0 & (currentSrcIndex = 0 );
    CurrentSrc = $ ("# myAudio source"). eq (currentSrcIndex). prop ("src ");
    MyAudio. src = currentSrc;
    MyAudio. play ();
    });

    In the event listener of the preceding button, we call the play () and pause () Methods of the original audio element to control the playing and pause of the audio. In addition, the audio element provides the currentSrc attribute, which indicates the file source of the current playback file. We set this attribute to control the song tracks currently played.

    Volume Control

    Next, we will add event listening to the two speakers on both sides of the volume bar, so that by clicking the two speakers on the left and right, we can reduce and increase the current playback volume. To set the volume of the player, we can call the volume attribute provided in the audio element. The maximum volume value is 1 and the minimum volume value is 0. Here, we increase or decrease the volume by 0.1 each time we click the horn. You can also use other values. However, it should be noted that the javascript language cannot provide precise control over decimal places. Therefore, each time the volume is reduced by 0.1, the actually reduced volume is slightly larger than 0.1, this results in the 0.09xxxx volume remaining when you click the reduce button for nine consecutive times. Then, you will find that the player cannot be muted... Of course, this problem can be easily solved (as shown below), just as a reminder.


    The Code is as follows:


    $ (". Volume_control. decrease"). click (function (){
    Var volume = myAudio. volume-0.1;
    Volume <0 & (volume = 0 );
    MyAudio. changeVolumeTo (volume );
    });
    $ (". Volume_control. increase"). click (function (){
    Var volume = myAudio. volume + 0.1;
    Volume> 1 & (volume = 1 );
    MyAudio. changeVolumeTo (volume );
    });

    In addition, we also need to use the slider or click a certain position of the volume bar to adjust the volume. With the above foundation, this is easy to complete. First, let's take a look at the function of adjusting the volume by clicking a certain position in the volume bar: click a certain position in the volume bar and calculate the length value from the starting point of the volume bar to this position, divide the value by the total length of the volume bar (100 here) to get the percentage value, multiply the percentage value by the maximum volume value 1 to get the volume value to be jumped to, and then assign the value to volume. The method of adjusting the volume by using a slider is similar to that of calculating the position of the slider in the volume bar. I will not explain it in detail here. If you have any questions, please leave a message below.


    The Code is as follows:


    $ (". Volume_control. base_bar"). mousedown (function (ev ){
    Var posX = ev. clientX;
    Var targetLeft = $ (this). offset (). left;
    Var volume = (posX-targetLeft)/100;
    Volume> 1 & (volume = 1 );
    Volume <0 & (volume = 0 );
    MyAudio. changeVolumeTo (volume );
    });
    $ (". Volume_control. slider"). mousedown (starDrag = function (ev ){
    Ev. preventDefault ();
    Var origLeft = $ (this). position (). left;/* Initial position of the slider */
    Var origX = ev. clientX;/* Initial mouse position */
    Var target = this;
    Var progress_bar = $ (". volume_control. progress_bar") [0];
    $ (Document). mousemove (doDrag = function (ev ){
    Ev. preventDefault ();
    Var moveX = ev. clientX-origX;/* calculate the distance of the mouse movement */
    Var curLeft = origLeft + moveX;/* the distance between the mouse and the slider */
    (CurLeft <-7) & (curLeft =-7 );
    (CurLeft> 93) & (curLeft = 93 );
    Target. style. left = curLeft + "px ";
    Progress_bar.style.width = curLeft + 7 + "% ";
    MyAudio. changeVolumeTo (curLeft + 7)/100 );
    });
    $ (Document). mouseup (stopDrag = function (){
    $ (Document). unbind ("mousemove", doDrag );
    $ (Document). unbind ("mouseup", stopDrag );
    });
    });

    Time Control

    Now the player is basically usable, but we want to skip part of the audio directly to a specific time point. How can this problem be achieved ??! The standards-making committee members are not fools. Such common functions will not be omitted ~ So please refer to the API. You will find that the audio element provides a property named currentTime, which is very concise and easy to understand (in fact, most of the attributes are well understood ), set this attribute to set the current playback time point.

    Here, we also need to use another audio attribute duration, which refers to the total time length of the current playback file. Therefore, based on the volume control implementation, we can do this:

    1. Click a position in the progress bar to calculate the percentage of the length from the start point of the progress bar to the total length of the progress bar (for example, click the center of the progress bar, the length from the start point to the position of the progress bar accounts for 50% of the total length of the progress bar.

    2. Multiply percentage by the total duration of the file duration to get the value of the time point you want to jump to, and then assign the value to currentTime to complete the function to be implemented.


    The Code is as follows:


    $ (". Time_line. base_bar"). mousedown (function (ev ){
    Var posX = ev. clientX;
    Var targetLeft = $ (this). offset (). left;
    Var percentage = (posX-targetLeft)/140*100;
    MyAudio. currentTime = myAudio. duration * percentage/100;
    });

    Here, the player has been basically formed. The remaining UI interaction implementation is irrelevant (in fact, it is the most important for me). If you are interested, go to the source code to view it. If you have any questions, please leave a comment below, hope you can exchange and learn more.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.