【问题标题】:How to add sound to HTML using Javascript? [duplicate]如何使用 Javascript 向 HTML 添加声音? [复制]
【发布时间】:2020-05-26 14:34:49
【问题描述】:

我使用以下代码向我的 HTML 文档添加声音。

function sound(src) {
    this.sound = document.createElement("audio");
    this.sound.src = src;
    this.sound.setAttribute("preload", "auto");
    this.sound.setAttribute("controls", "none");
    this.sound.style.display = "none";
    document.body.appendChild(this.sound);
    this.play = function(){
      this.sound.play();
    }
    this.stop = function(){
      this.sound.pause();
    }
}
var bite = new sound("Bite+2.mp3");
var slurp = new sound("Slurp+4.mp3");
var hiss = new sound("snakehiss2.mp3");
var bg = new sound("gameSound.mp3");

现在,我想设置 'bg' 的音量并使其 loop = true 我想不出办法来做到这一点。请帮忙!!
另外,我在网上某处看到了代码,但我无法理解“this.sound”是如何工作的。请解释一下
编辑:
事实证明,我只需要执行 bg.sound.volume = 0.5 即可。同样,bg.sound.loop = true。
我也理解“this”的用法,但为什么需要“sound”?用 this.src 代替 this.sound.src 还不够吗?

【问题讨论】:

  • 请限制自己每个问题回答一个问题。 this.sound 是使用 JavaScript 的标准面向对象编程。见stackoverflow.com/questions/3127429/…
  • 要循环播放音频,请参阅Loop audio with JavaScript
  • @HereticMonkey 我确实阅读了这些问题,但我想知道如何用我实现它的方式来做到这一点。另外,我理解“this”,但我不明白在“this.sound”中将“sound”与“this”一起使用。我认为最好只问一个问题,而不是复制代码单独提出一个问题,但如果这里是这样做的,我会从下一次开始处理它。
  • this.sound 指的是在sound 的第一行创建的audio 元素。设置this.src 不会设置audio 元素的src 属性,从而违背了目的......您将包装函数/对象命名为与内部属性名称相同,这让您感到困惑。如果你称它为this.audio,并留下外部函数sound,也许更有意义?

标签: javascript html audio


【解决方案1】:

我是 stackoverflow 的新手,但这是我的猜测

首先声明全局变量

let siteSound;

然后像你一样声明函数

function createSound(src) { //src stands for source
  this.sound = document.createElement("audio");
  this.sound.src = src;
  this.sound.setAttribute("preload", "auto");
  this.sound.setAttribute("controls", "none");
  this.sound.style.display = "none";
  document.body.appendChild(this.sound);
  this.play = function(){
    this.sound.play();
  }
  this.stop = function(){
    this.sound.pause();
  }
}

然后创建一个启动声音的函数

function startSound(dirToSound){
   siteSound = new createSound();
   siteSound.play();
}

如您所见,我们将变量 siteSound 声明为新的 createSound(),并且在该 createSound() 方法中,“this”指的是导致该方法的变量,在我们的原因中是 siteSound。 (this.sound 代表 siteSound.soundthis.sound.src 代表 siteSound.sound.src 等)

由于网站播放音乐的方式受到限制(用户必须先在页面上做某事,例如单击),我想您使用某种事件来创建音乐,例如添加事件侦听器“onclick”为鼠标,并调用函数startSound:

someHTMLElement.domElement.addEventListener('onclick', startSound(dirToSound), false);

当您单击 someHTMLElement 时,应调用 startSound(dirToSound) 并开始播放音乐。

如果你想循环,在方法createSound(src)中添加this.loop = true(我不确定是否是html,但这在其他地方有效,你必须测试)

如果您想将响度调整为 50%,请尝试将 this.volume = 0.5 添加到 createSound(src) 函数(从 0 到 1 的数字)

【讨论】:

  • 感谢您尝试帮助 OP,但每当您看到 cmets 以“这是否回答您的问题?”开头时这通常意味着网站上还有其他问题已经回答了该问题(在这种情况下,其中有两个问题,因为有两个问题 [嗯,3] 被问到)。请参阅the help center 了解更多信息。否则,一个很好的答案,所以请继续回答(以前未提出的)问题!
  • 但是这样,loop = true 将是通用的。我希望它只出现在选定的声音中。我找到了一种方法,bg.sound.loop=true。另外,我理解您对此的解释,但为什么需要 this.sound.src 等?为什么 this.src 不够用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多