audio - 网络音频声音未播放

标签 audio web-applications safari volume web-audio-api

我有以下代码来播放加载到缓冲区中的特定声音。

sound1.gainNode = context.createGain();
sound1.source = context.createBufferSource();
sound1.source.buffer = soundBuffers[num];
sound1.source.connect(sound1.gainNode);
sound1.gainNode.connect(context.destination);
sound1.source.looping = true;
sound1.source.start(0);

这是我所谓的更改音量方法:
<input type="range" min="0" max="100" value="70" id="playBtn1_vol" onchange="changeVolume(this,sound1Gain)" style="display:none">

这是更改量方法:
function changeVolume = function(element,soundNo){
      var volume = element.value;
      var fraction = parseInt(element.value) / parseInt(element.max);
      // Using an x^2 progression as it gives a better sound than linear.
      soundNo.gainNode.gain.value = fraction * fraction;
    };

在尝试使用增益使音量工作之前,它的运行情况还不错,但现在已经坏了,我找不到问题所在。如果有人能指出我正确的方向,将不胜感激。

最佳答案

这是示例代码(js),可以为您提供帮助。 Reference

var VolumeSample = {
};

// Gain node needs to be mutated by volume control.
VolumeSample.gainNode = null;

VolumeSample.play = function() {
  if (!context.createGain)
    context.createGain = context.createGainNode;
  this.gainNode = context.createGain();
  var source = context.createBufferSource();
  source.buffer = BUFFERS.techno;

  // Connect source to a gain node
  source.connect(this.gainNode);
  // Connect gain node to destination
  this.gainNode.connect(context.destination);
  // Start playback in a loop
  source.loop = true;
  if (!source.start)
    source.start = source.noteOn;
  source.start(0);
  this.source = source;
};

VolumeSample.changeVolume = function(element) {
  var volume = element.value;
  var fraction = parseInt(element.value) / parseInt(element.max);
  // Let's use an x*x curve (x-squared) since simple linear (x) does not
  // sound as good.
  this.gainNode.gain.value = fraction * fraction;
};

VolumeSample.stop = function() {
  if (!this.source.stop)
    this.source.stop = source.noteOff;
  this.source.stop(0);
};

VolumeSample.toggle = function() {
  this.playing ? this.stop() : this.play();
  this.playing = !this.playing;
};

关于audio - 网络音频声音未播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117042/

相关文章:

java - 如何将 JProfiler 连接到在本地主机上运行的应用程序?

javascript - 强制网页的所有打开实例在 Google Chrome 网络应用程序中刷新

web-applications - Node.js:可用/创建 Web 代理

javascript - JS Keycode 与 Safari mobile 是一样的

IOS 12 PWA 支持

android - 在没有麦克风的情况下在Android中录制音频流

java - 用Java确定给定时间内的频率

iphone - 仅使用 iPhone 顶部的麦克风

html - 我无法在Firefox中播放.wav文件。

html - Safari 不运行 css 显示 :none property