如何使用 javascript 和 html 循环 mp3 文件
How can I loop a mp3 file with javascript and html
感谢您查看,今天我决定在我的网站上添加一些声音,这就是它出错的地方,重点是按下按钮,而不是 mp3 应该循环,我知道它适用于 wav,但它不适用于 mp3,所以这是我的问题。我可以调整脚本以使mp3循环吗?
JavaScript:
<script language="javascript" type="text/javascript">
function playSound(soundfile) {
var type_attr = '';
if (navigator.userAgent.indexOf('Firefox') != -1) {
type_attr = "type='"application/x-mplayer2'"";
}
document.getElementById("dummy").innerHTML = "<embed src='"" + soundfile + "'" hidden='"true'" autostart='"true'" loop='"true'" " + type_attr + " />";
}
</script>
目录:
<span id="dummy">
<tr>
<td><input type="button" class='gstring' value="G" width="50" height="50" onClick="playSound('violin/Gstring.mp3')"></td>
<td><input type="button" class='gstring' value="D" width="50" height="50" onClick="playSound('violin/Dstring.mp3')"></td>
<td><input type="button" class='gstring' value="A" width="50" height="50" onClick="playSound('violin/Astring.mp3')"></td>
<td><input type="button" class='gstring' value="E" width="50" height="50" onClick="playSound('violin/Estring.mp3')"></td>
<td><input type="button" class='g2string' value="STOP" width="70" height="50" onClick="playSound('')"></td>
</tr>
</span>
我不想使用 wav 的原因是因为声音文件是 +/- 1mb(我想使用很多),而且我负担不起一个大的托管计划,所以我宁愿使用 mp3 代替。我希望有可能。
谢谢,迪德里克。
在此网页上,他们描述了如何播放mp3声音的不同解决方案。我试过了HTML5音频元素,它甚至可以用于他们那里的mp3循环(有时加载mp3需要一段时间,但几秒钟后声音开始循环)
<!DOCTYPE html>
<html>
<script>
function play_pause() {
var myAudio = document.getElementById("myAudio");
if (myAudio.paused) {
myAudio.play();
} else {
myAudio.pause();
}
}
</script>
<body>
<button type="button" onclick="play_pause()">Play/Pause</button>
<audio id="myAudio" preload loop>
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support this audio format.
</audio>
</body>
相关文章:
- jQuery:循环一个具有不同超时值的循环
- 在循环中分配json值时,值被覆盖
- 如何在下面的ES6循环中获得前面的文本
- 为什么“;未定义的“;在JavaScript中结束循环
- Javascript循环不会自我更新
- 如何使用jquery处理php循环通过元素
- 而循环只设置php中输入字段中的第一个值
- 循环遍历数组中的特定索引
- Javascript返回值只在循环中返回一次
- 按照选项卡索引的顺序循环一个jQuery选择
- 循环遍历以数组为值的Javascript对象
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 循环结束/推送到数组之前在页面上呈现EJS
- 循环比赛位置算法
- jQuery循环在特定位置暂停
- 我的javascript for循环不起作用
- 循环浏览多个身体背景图像
- 如何使用 javascript 和 html 循环 mp3 文件
- 循环mp3跨浏览器