Home >Web Front-end >JS Tutorial >How to add music with JavaScript

How to add music with JavaScript

藏色散人
藏色散人Original
2021-04-08 10:44:337463browse

How to add music in JavaScript: first create a js sample file; then define an autoPlay method; finally introduce the music file through "myAuto.src='/media/v2/sss.mp3';" .

How to add music with JavaScript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

Adding music to the page through js

The code is as follows! Compatible with IE

// JavaScript Document
function autoPlay(){//自动播放
     var myAuto = document.getElementById('myaudio');
     myAuto.src = '/media/v2/sss.mp3';//MP3路径
     myAuto.play();
}
function pausePlay(){//暂停播放
  var myAuto = document.getElementById('myaudio');
   myAuto.pause(); 
}
function createAuto(){
    var _id = $("#audio");
    if (window.applicationCache) {
         _id.html(&#39;<audio id="myaudio" src="" controls="controls" loop="false" hidden="true"></audio>&#39;)
            } else {
            _id.html(&#39;<embed src="" id="myaudio" style="display: none;"></embed>&#39;); 
       }
 }
创建方式
createAuto();

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to add music with JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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