【问题标题】:Distorted sounds with Dartium and Web Audio API in DartDartium 和 Dart 中的 Web Audio API 失真的声音
【发布时间】:2012-10-07 12:58:57
【问题描述】:

我对 HTML5 音频 api 还很陌生:我在 HTML5 Rocks 上阅读了一些相关文章,但有时在 Javascript 和 Dart 之间切换可能有点棘手。

无论如何,我一直在 Dart 中试验 HTML5 音频。为了给一个简单的游戏产生声音效果,我创建了一个如下的类。我创建了一个 AudioContext,将声音数据加载到 SoundBuffers 中,当需要播放声音时,创建了一个 AudioBufferSourceNode,通过它来播放存储在缓冲区中的数据:

class Sfx {
  AudioContext audioContext;
  List<Map> soundList;
  int soundFiles;

  Sfx() {
    audioContext = new AudioContext();
    soundList = new List<Map>();
    var soundsToLoad = [
      {"name": "MISSILE", "url": "SFX/missile.wav"},
      {"name": "EXPLOSION", "url": "SFX/explosion.wav"}
    ];
    soundFiles = soundsToLoad.length;

    for (Map sound in soundsToLoad) {
      initSound(sound);
    }
  }

  bool allSoundsLoaded() => (soundFiles == 0);

  void initSound(Map soundMap) {
    HttpRequest req = new HttpRequest();
    req.open('GET', soundMap["url"], true);
    req.responseType = 'arraybuffer';
    req.on.load.add((Event e) {
      audioContext.decodeAudioData(
        req.response,
        (var buffer) {
            // successful decode
            print("...${soundMap["name"]} loaded...");
            soundList.add({"name": soundMap["name"], "buffer": buffer});
            soundFiles--;
        },
        (var error) {
          print("error loading ${soundMap["name"]}");
        }
      );
    });
    req.send();
  }

  void sfx(AudioBuffer buffer) {
    AudioBufferSourceNode source = audioContext.createBufferSource();
    source.connect(audioContext.destination, 0, 0);
    source.buffer = buffer;
    source.start(0);
  }

  void playSound(String sound) {
    for (Map m in soundList) {
      print(m);
      if (m["name"] == sound) {
        sfx(m["buffer"]);
        break;
      }
    }
  }
}

(音效在文件夹“SFX”中。现在我查看了代码,可能有上百万种更好的方法来组织数据,但现在不是重点。)我可以播放声音通过创建一个 Sfx 实例并调用 playSound 方法来实现效果。

例如

#import('dart:html');

#source('sfx.dart');

Sfx sfx;
void main() {
  sfx = new Sfx();
  window.on.keyUp.add((KeyboardEvent keX) {
    sfx.playSound("MISSILE");
  });
}

(编辑:添加代码以在按键时播放声音。)

问题是:尽管使用 dart2js Javascript,声音效果在 Safari 中按预期播放,但在 Dartium 或(使用 dart2js Javascript)在 Chrome 中播放时,它们会失真。 (在 Firefox 中,问题更严重!)

有什么明显的事情是我忽略或需要考虑的吗?否则,是否有任何参考或教程(最好是在 Dart 上下文中)可能有所帮助?

【问题讨论】:

  • Dart 团队有一个git repo,他们正在将 HTML5 Rocks 教程移植到 Dart。其中之一是Getting Started with the Web Audio API 的端口。不确定这是否是您所追求的,但如果您想看看他们是如何做到的,那就是here
  • 谢谢!这听起来是一个很好的起点。我一定会去看看的。
  • 作为更新:我查看了上面链接的 git repo;问题似乎出在我使用的音频文件上:它是 32 位 .wav,而示例中的声音是 16 位。当我将声音文件保存为 16 位 .wav 文件时,它们在 Chrome 和 Safari 中都按预期播放...
  • 太棒了!我注意到他们已经为 pub 重新安排了他们的库,所以到端口的链接之一已经改变了一些。现在是here

标签: html audio dart


【解决方案1】:

感谢您试用 Dart!

首先,Firefox 不支持 Web Audio API(还没有?)Chrome 和 Safari 支持 Web Audio API。您可以在此处跟踪 Web Audio API 的采用情况:http://caniuse.com/#feat=audio-api

其次,请在 Dartium 中尝试这个 Web Audio API 示例:https://github.com/dart-lang/dart-html5-samples/tree/master/web/webaudio/intro 您需要先克隆 repo 并在本地运行它。此示例适用于本地。

这听起来更像是一个错误报告。如果 dart-html5-samples 中的示例对您有用,但您的上述代码仍然存在失真,请在 http://dartbug.com/new 处打开错误,以便我们查看。

需要考虑的一件事是等到特定的 MISSLE 声音加载完毕后再连接 keyUp 处理程序。

【讨论】:

  • 那么这就解释了 Firefox!至于等待声音加载:加载声音时会打印一条消息,以便我知道何时可以开始测试。感谢您的链接!在我称它为错误之前,我会先尝试一下。
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 2013-01-03
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多