<!DOCTYPE html>
<html lang="en">
<head>
<title>js 控制 HTML5 audio标签 播放音频文件</title>
</head>
<body>
<button onclick="play()">播放音频</button>
<button onclick="pause()">暂停音频</button>
<button onclick="stop()">停止音频</button>
<button onclick="skip()">跳到第10秒</button>
<script type="text/javascript">
var audio = document.createElement("audio");
audio.src = "alarm.mp3";
audio.controls = true
document.body.append(audio)
function play() {
audio.play();
}
function pause() {
audio.pause();
}
function stop() {
audio.currentTime = 0;
audio.pause();
}
function skip() {
audio.currentTime = 10;
audio.play();
}
</script>
</body>
</html>