A simple music player with JS implementation

Source: Internet
Author: User
Tags event listener

<HTML><Head>    <MetaCharSet= "Utf-8">    <Metaname= "Author"content= "Dongfeng">    <title></title>    <Script>    varmusics= [    '1.mp3',    '2.mp3',    '3.mp3',    '4.mp3',    '2.mp3'];//defines the index of the audio being playedvarIndex= 0;//sequence of records and random variablesvarPlaytype;window.onload= function() {    varTypesel=document.getElementById ('Typesel'); //change the playback mode when the user changes the drop-down menu optionTypesel.onchange= function() {console.log (typesel.value) Window.playtype=Typesel.value; }    varplayer=document.getElementById ('player'); //the first file that plays when a page loadsplayer.src=Musics[index]; Player.addeventlistener (  'ended', myFunction) functionmyFunction () {if(Playtype = = ' Random ') {Index=Math.floor (Math.random ()*musics.length); Console.log (index) PLAYER.SRC=Musics[index]; } Else {            //Play next Audioplayer.src=musics[++Index%Musics.length]} player.play (); }}    </Script>    <style>#typeSel{Margin-bottom:30px;width:160px;}</style></Head><Body><H2>Music player</H2><SelectID= "Typesel">    <optionvalue= "sequence">Sequential playback</option>    <optionvalue= "random">Random Play</option></Select><BR/><AudioID= "Player"Controls>Your browser does not support the audio element</Audio></Body></HTML>

The Onended method in audio does not know how to use the,<element onended= "myScript" > and Objectonly with the event listener method. onended= function () {myScript}; none works, only object. AddEventListener ("ended", myScript);

A simple music player with JS implementation

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.