1.引入soundjs文件
<script src="https://code.createjs.com/1.0.0/soundjs.min.js"> </script>
2.加载和播放音乐有很多种方式, 由于我们想重复使用这个声音,我们将在body标签中加载一次,并在任何时候单击我们所想点击的标签来播放他
<body onload="loadSound();"> <button onclick="playSound();" class="playSound">play Sound</button> </body>
3.首先拿一个声音文件 然后为其定义一个id
var soundID = "Thunder";
4.现在来加载这个音频 , 首先在文件头部访问sound这个类, 让后调用registerSound方法,然后传入文件的路径,和id,
function loadSound() { createjs.Sound.registerSound("./builds/sound/Thunder1",soundID); }
5.要是想播放音频, 需要调用play()方法, 并且传入音频的soundID
function playSound() { createjs.Sound.play(soundID); }
6.这样我们每次点击按钮的话就可以 播放这个视频
7.代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.createjs.com/1.0.0/createjs.min.js"> </script>
<script src="https://code.createjs.com/1.0.0/soundjs.min.js"> </script>
<script>
var soundID = "Thunder";
function loadSound() {
createjs.Sound.registerSound("./builds/sound/Thunder1.mp3",soundID);
}
function playSound() {
createjs.Sound.play(soundID);
}
</script>
</head>
<body onload="loadSound();">
<button onclick="playSound();" class="playSound">play Sound</button>
</body>
</html>
8.音频文件 自己随便弄一个MP3格式的就可以, 路径需要自己修改下,改成你的文件存放位置也可以