javascript – 创建无缝的音频循环 – Web

前端之家收集整理的这篇文章主要介绍了javascript – 创建无缝的音频循环 – Web前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想创建一个音频文件的无缝循环.但到目前为止我使用的所有方法中,终点和结束之间存在明显的差距.开始.

这是我到目前为止所尝试的:

第一种方法是使用HTML中的音频并且循环但是从轨道的末尾开始到开始时仍然存在明显的延迟.

然后我用JavaScript尝试了相同的结果:

  1. let myAudio = new Audio(file);
  2. myAudio.loop = true;
  3. myAudio.play();

之后我尝试了这个(根据this answer)

  1. myAudio.addEventListener(
  2. 'timeupdate',function() {
  3. var buffer = .44;
  4. if (this.currentTime > this.duration - buffer) {
  5. this.currentTime = 0;
  6. this.play();
  7. }
  8. },false
  9. );

我玩缓冲区但我只是为了缩小差距而不是完全放弃它.

我转向图书馆SeamlessLoop(GitHub)并让它在Chromium浏览器中无缝循环(但不是在最新的Safari中.没有在其他浏览器中测试).我用的代码

  1. let loop = new SeamlessLoop();
  2. // My File is 58 Seconds long. Btw there aren't any gaps in the file.
  3. loop.addUri(file,58000,'sound1');
  4. loop.callback(soundsLoaded);
  5. function soundsLoaded() {
  6. let n = 1;
  7. loop.start('sound' + n);
  8. }

编辑:我尝试了另一种方法:通过两个不同的音频元素循环:

  1. var current_player = "a";
  2. var player_a = document.createElement("audio");
  3. var player_b = document.createElement("audio");
  4. player_a.src = "sounds/back_music.ogg";
  5. player_b.src = player_a.src;
  6. function loopIt(){
  7. var player = null;
  8. if(current_player == "a"){
  9. player = player_b;
  10. current_player = "b";
  11. }
  12. else{
  13. player = player_a;
  14. current_player = "a";
  15. }
  16. player.play();
  17. /*
  18. 3104.897 is the length of the audio clip in milliseconds.
  19. Received from player.duration.
  20. This is a different file than the first one
  21. */
  22. setTimeout(loopIt,3104.897);
  23. }
  24. loopIt();

但是,由于浏览器中的毫秒数不够一致或不够精细,这不能很好地工作,但它确实比音频的正常“循环”属性好得多.

任何人都可以引导我走向正确的方向来无缝循环音频吗?

最佳答案
您可以改用Web Audio API.这有几点需要注意,但它可以让你准确地循环到单个样本级别.

需要注意的是,您必须将整个文件加载到内存中.对于大文件,这可能不实用.如果文件只有几秒钟,那么应该没有任何问题.

第二个是你必须手动编写控制按钮(如果需要),因为API有一个低级方法.这意味着播放,暂停/停止,静音,音量等.扫描和可能暂停可能是他们自己的挑战.

最后,不是所有的浏览器support Web Audio API – 在这种情况下你将不得不回退到常规的音频API甚至Flash,但如果你的目标是现代浏览器,这应该不是现在的主要问题.

这将加载一个4巴的鼓环,并在循环播放时没有任何间隙.主要步骤是:

>它从启用CORS的源加载音频(这很重要,要么使用与您的页面相同的域,要么设置外部服务器以允许跨域使用,如本例中DropBox为我们所做的那样).
> AudioContext然后解码加载的文件
>解码文件用于源节点
>源节点连接到输出
>启用循环并从内存中播放缓冲区.

  1. var actx = new (AudioContext || webkitAudioContext)(),src = "https://dl.dropBoxusercontent.com/s/fdcf2lwsa748qav/drum44.wav",audioData,srcNode; // global so we can access them from handlers
  2. // Load some audio (CORS need to be allowed or we won't be able to decode the data)
  3. fetch(src,{mode: "cors"}).then(function(resp) {return resp.arrayBuffer()}).then(decode);
  4. // Decode the audio file,then start the show
  5. function decode(buffer) {
  6. actx.decodeAudioData(buffer,playLoop);
  7. }
  8. // Sets up a new source node as needed as stopping will render current invalid
  9. function playLoop(abuffer) {
  10. if (!audioData) audioData = abuffer; // create a reference for control buttons
  11. srcNode = actx.createBufferSource(); // create audio source
  12. srcNode.buffer = abuffer; // use decoded buffer
  13. srcNode.connect(actx.destination); // create output
  14. srcNode.loop = true; // takes care of perfect looping
  15. srcNode.start(); // play...
  16. }
  17. // Simple example control
  18. document.querySelector("button").onclick = function() {
  19. if (srcNode) {
  20. srcNode.stop();
  21. srcNode = null;
  22. this.innerText = "Play";
  23. } else {
  24. playLoop(audioData);
  25. this.innerText = "Stop";
  26. }
  27. };

猜你在找的JavaScript相关文章