search
HomeWeb Front-endH5 TutorialHTML5 graphic tutorial on making a cool audio player plug-in_html5 tutorial skills

The picture above is the UI interface diagram of this audio player, which also hides a playlist of songs. The entire player UI is drawn with CSS and font-face except for the background portrait and the star rating of the song, which are all drawn with CSS and font-face. The only areas that seem to be difficult are the production of CDs and disc players. In the song Both the CD and the disc player have animated interactive effects during playback, which will be explained in a later section. Click on the CD in the player to open the playlist~

Because this tutorial is mainly to demonstrate the use of the new

Main functions:

1. Play, pause, previous song, next song, volume increase or decrease

2. Click CD to open and close the playlist

3. You can drag local music files to the player to play

Html structure


Copy code
The code is as follows:

< ;div id="myAudio" style="margin:0 auto;">








    e
    c> ;
    d


    0:00



    0:00



    audio tag

    In the above structure we can find the new audio tag in HTML5, which has a controls attribute. As the name suggests, it is the controller of the player. The controls attribute specifies that the browser provides playback controls for audio, such as in the chrome browser If you set this attribute in the audio tag under


    The code is as follows:

    However, due to the different rendering effects of audio tags under different browsers, this simple method is not suitable for use under cross-browsers, and the functions provided by the browser's default player control are really too limited. Less. . Therefore, we usually hide the browser's default playback controls by not setting this attribute, and manually add additional tags and styles to customize the player's UI interface.

    Playback Controls

    After the UI interface of the player is drawn, the first thing we need to do is to add corresponding event listeners for the three main control buttons: play, previous song, and next song.


    Copy code
    The code is as follows:

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


    Copy code
    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 currentSrc = $("#myAudio source").eq(currentSrcIndex).prop("src");
    myAudio.src = currentSrc;
    myAudio.play();
    });

    In the above button click event monitoring, we control the play and pause of the audio by calling the play() and pause() methods of the original audio element. In addition, the audio element provides the currentSrc attribute, which represents the file source of the currently playing file. We control the currently playing song track by setting this attribute.

    Volume control

    Next, let’s add event monitoring to the two small speakers on both sides of the volume bar, so that the volume of the current playback can be reduced and increased by clicking on the two small speakers on the left and right. 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 is 0. Here we control the volume by increasing or decreasing the volume by 0.1 each time we click the speaker. Of course you can also use other values. But it should be noted that the JavaScript language cannot provide precise control over decimals, so each time the volume is reduced by 0.1, the volume actually reduced is slightly greater than 0.1, which results in 0.09 remaining when the volume reduction button is clicked 9 times in a row. xxxx volume, and then you will find out why the player cannot be muted. . . Of course this problem is easy to fix (shown below), just a little reminder.


    Copy code
    The code is as follows:

    $(".volume_control .decrease") .click(function() {
    var volume = myAudio.volume - 0.1;
    volume 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 implement the function of using the slider or clicking a certain position of the volume bar to control the volume. With the above foundation, this is easy to complete. . First, let's take a look at the function of clicking a certain position of the volume bar to control the volume: click a certain position of the volume bar, calculate the length value from the starting point of the volume bar to that position, and then divide this value by the total length of the volume bar. (Here it is 100) Get the percentage value, then multiply the percentage value by the maximum volume value 1 to get the volume value you want to jump to, and then assign it to volume. The method of controlling the volume through a slider is similar to this. The main thing is to know how to calculate the position value of the slider on the volume bar. I won’t explain it in detail here. If you have any questions, please leave a message below.


    Copy code
    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 myAudio.changeVolumeTo(volume );
    });
    $(".volume_control .slider").mousedown(starDrag = function(ev) {
    ev.preventDefault();
    var origLeft = $(this). position().left; /*Initial position of slider*/
    var origX = ev.clientX; /*Initial position of mouse*/
    var target = this;
    var progress_bar = $(".volume_control .progress_bar")[0];
    $(document).mousemove(doDrag = function(ev){
    ev.preventDefault();
    var moveX = ev.clientX - origX; /*Calculate mouse Moving distance*/
    var curLeft = origLeft moveX; /*Use the distance moved by the mouse to represent the moving distance of the slider*/
    (curLeft (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

    Okay, now the player is basically working, but we also want to be able to directly skip a part of the audio to a specific time point. So how to achieve it? ? ! The committee members who formulate the standard are not fools. Such commonly used functions cannot be omitted. So quickly browse the API and you will find that the audio element provides an attribute called currentTime, which is a very concise and easy-to-understand name. (In fact, most attributes are easy to understand). Setting this attribute can set the current playback time point.

    Here, we also need to use another attribute of audio, duration, which refers to the total time length of the currently playing file. So depending on the volume control implementation, we can do this:

    1. Click a certain position of the progress bar, and calculate the percentage value of the length from the starting point of the progress bar to that position to the total length of the progress bar (for example, click on the middle position of the progress bar, then the length from the starting point of the progress bar to that position will be The length of the position accounts for 50% of the total progress bar length), recorded as percentage.

    2. Then multiply the percentage by the total duration of the file 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.


    Copy code
    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;
    });

    At this point, the player has basically taken shape. There are some insignificant UI interaction implementations left (actually, they are the most important to me, haha). If you are interested, you can check it out in the source code. If you have any questions, you can leave a comment below. I hope we can communicate and learn more.

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    html5的div一行可以放两个吗html5的div一行可以放两个吗Apr 25, 2022 pm 05:32 PM

    html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

    html5中列表和表格的区别是什么html5中列表和表格的区别是什么Apr 28, 2022 pm 01:58 PM

    html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

    html5怎么让头和尾固定不动html5怎么让头和尾固定不动Apr 25, 2022 pm 02:30 PM

    固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

    HTML5中画布标签是什么HTML5中画布标签是什么May 18, 2022 pm 04:55 PM

    HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

    html5中不支持的标签有哪些html5中不支持的标签有哪些Mar 17, 2022 pm 05:43 PM

    html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

    html5废弃了哪个列表标签html5废弃了哪个列表标签Jun 01, 2022 pm 06:32 PM

    html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

    html5是什么意思html5是什么意思Apr 26, 2021 pm 03:02 PM

    html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

    Html5怎么取消td边框Html5怎么取消td边框May 18, 2022 pm 06:57 PM

    3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment