mp3的播放

2023-12-13 03:04:56

1.这段vue代码会播放声音,但是会有audio标签

<template>
  <div>
    <audio id="myAudio" controls>
      <source src="./test.mp3" type="audio/mp3" />
      Your browser does not support the audio tag.
    </audio>
  </div>
</template>
<script>
export default {
  mounted() {
    // 播放
    var audio = document.getElementById("myAudio");
    audio.play();
  }
}
</script>

2.只有不会显示video标签的播放

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Play MP3 on Button Click</title>
</head>
<body>

<button id="playButton">Play MP3</button>
<script>
  // 创建 Audio 对象
  const audio = new Audio('./test.mp3');
  // 获取按钮元素
  const playButton = document.getElementById('playButton');
  // 点击按钮时播放音频
  playButton.addEventListener('click', function() {
    audio.play();
  });
</script>

</body>
</html>

文章来源:https://blog.csdn.net/m0_47999208/article/details/134807602
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。