繁体   English   中英

如何将此代码应用于我的音频

[英]How to apply this code to my audio

编辑*我这样做:

   if (vol > 0) {
     vol -= 0.05;
     document.getElementById("introMusic").volume = vol;
   }

我收到此错误:未捕获的错误:IndexSizeError:DOM异常1 preGameContentObject.js:102(匿名函数)。 它会降低声音,直到声音非常低,然后继续播放。

我从来没有用javaScript做过音频,而且非常菜鸟。 这个问题几乎令人尴尬。 我在该网站上发现了这项强大的功能,可以淡出我的乒乓球比赛的开场曲。

我通过此功能开始音乐:

setUpGame: function() {
        this.setUpPaddles();
        this.setUpBall("left");
        preGameContent.getMouseHover();
        preGameContent.drawButtons();
        preGameContent.getMouseClick();
        document.getElementById('introMusic').play();
    },

那是最佳实践吗? 有用。

我想知道如何将此功能应用于我的代码:

// Initial volume of 0.20
// Make sure it's a multiple of 0.05
var vol = 0.20;
var interval = 200; // 200ms interval

var fadeout = setInterval(
  function() {
    // Reduce volume by 0.05 as long as it is above 0
    // This works as long as you start with a multiple of 0.05!
    if (vol > 0) {
      vol -= 0.05;
      audio.setVolume(vol);
    }
    else {
      // Stop the setInterval when 0 is reached
      clearInterval(fadeout);
    }
  }, interval);

我知道在哪里调用它,因为我有一个“如果单击游戏按钮-启动游戏”这种类型的功能,并且可以在其中弹出它。 我在哪里将其引用到我的“ introMusic”元素ID? 谢谢!

我改用它是因为它没有错误(需要稍微重构):

function fadeVolume(volume, callback)
                    {
                        var factor  = 0.02,
                            speed   = 50;
                        if (volume > factor)
                        {
                            setTimeout(function(){
                                fadeVolume((document.getElementById("introMusic").volume -= factor), callback);         
                            }, speed);
                        } else {
                            (typeof(callback) !== 'function') || callback();
                        }
                    }
                    fadeVolume(document.getElementById("introMusic").volume, function(){
                        console.log('fade complete');
                        document.getElementById("introMusic").pause();
                        document.getElementById("introMusic").currentTime = 0;
                    });

要停止音频,我使用了.pause(); 并重置当前时间,因为.stop(); 不工作。 有人知道为什么吗? 谢谢。

暂无
暂无

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

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