HTML5 playing and pausing music using the audio tag

Source: Internet
Author: User
Tags jquery library

You can try listening and pausing with a list of music you've recently worked on and click Audition. In fact, HTML5 implementation of this function is very simple.

Embed the outer chain of music in the Web page first

<ahref= "javascript:void (0);"onclick= "Music.changeclass (this, ' media ');"class= "Btn Btn-green"Data-id= "Edit_btn"Data-width= "The "Data-height= " the"Data-mask= "true"Data-title= "Audition">Try Listening</a><Audiopreload= "preload"src= "Http://www.baidu190.com/md5/043546aee51d1f85a0b871e8406750352e.mp3"ID= "Media"Hidden></Audio>

You can control the music playback (play ()) and Pause (Pause ()) by using the audio ID, and by changing the word on the button (my button is done with a tag), JS code (remember to introduce the jquery library):

 //  music play   music = function   (Target,id) { var  htmlvalue =
     
       $ (target). html ();        
      var  ids = document.getElementById (ID); (Htmlvalue  = = ' Audition ' ) ?         Ids.play (): Ids.pause (); (Htmlvalue  = = ' Audition ' ) ?     $ (target). html ("pause"  "audition"   

Principle: JS First get the button (A-tag) object and audio object, and then judge the button's HTML () value to achieve the music pause and play. About implementing different buttons to listen to different music, let the audio ID be the primary key of the row data .  

HTML5 playing and pausing music using the audio tag

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.