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

How to add music with JavaScript

藏色散人
Release: 2023-01-04 09:35:05
Original
7418 people have browsed it

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();
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template