繁体   English   中英

用javascript / html制作音频文件

[英]making a audio file sound in javascript/html

我正在尝试播放.ogg文件,我也将其转换为.mp3文件

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
    </head>
    <body>


<body onclick = "playSound('file:///C:/Users/Rehaan/Downloads/myfirstrecording%20(3).ogg');">

  <p id = "sound">
  </p>
  Click here to play the sound!
    </body>
</html>

这是java脚本文件,

function playSound( url ){   
  document.getElementById("sound").innerHTML="<embed src='"+url+"' hidden=true autostart=true loop=false>";
}

我建议使用HTML5音频标签。

      <audio id="audio_samples" src="mymp3audiofile.mp3" controls></audio> 

音频标签支持mp3,ogg和wav音频文件。



此外,这里有Javascript音频控制功能,你可能会觉得有用:

      <script type="text/javascript">
        var audio = document.getElementById("audio_samples");


        function playAudio() {
            audio.play();    
        }

        function pauseAudio() {
            audio.pause();    
        }

        function restartAudio() {
            audio.load(); 
            audio.play();
        }

        function louder() {
            audio.volume += 0.1;
        }

        function softer() {
            audio.volume -= 0.1;
        }
      </script>

      <button onclick="playAudio()">Play</button>
      <button onclick="pauseAudio()">Pause</button>
      <button onclick="restartAudio()">Restart</button>
      <button onclick="louder()">Louder</button>
      <button onclick="softer()">Softer</button>

我希望这有帮助!

您可以使用<audio> HTML5标签。 它可以播放.mp3,.ogg,.wav格式。 这是我从w3schools获得的一个例子。

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio tag.
</audio>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM