【问题标题】:Save audio stream / Uint8List data in file flutter将音频流/Uint8List 数据保存在文件颤动中
【发布时间】:2020-08-12 09:37:00
【问题描述】:

我从 Android 和 iOS 的记录器插件中获得 Uint8List。每当我在麦克风的流订阅中获取数据时,我都想将数据写入本地可播放的音频文件。有没有办法写数据?

目前,我正在存储数据,例如

recordedFile.writeAsBytesSync(recordedData, flush: true);

它正在将数据写入文件,但无法从文件存储中播放。但是,如果我读取相同的文件并将其字节提供给插件,则它正在播放相同的缓冲区。

【问题讨论】:

  • 你做到了吗?
  • @FilipeOS 是的。我必须添加一些标题,它允许我编写一个可播放的音频文件
  • @Dhalloo 我可以知道哪个插件正在使用,哪个正在返回字节吗?
  • @UttamPanchasara 我使用flutter sound 插件来获取字节并从字节中播放。
  • @Dhalloo 你能分享一下代码吗? flutter_sound 是如何返回字节的?

标签: android flutter dart buffer audio-recording


【解决方案1】:

我在将我的字节写入文件之前添加了一个 WAVE/RIFF Header,它将元数据提供给字节和文件。

我的流中有 16 位 PCM 音频字节和 MONO 通道。当我收到字节时,我会将这些字节附加到字节列表中。

List<int> recordedData = [];
recordedData.addAll(value);

现在我的列表中有所有记录的字节。 停止我的录音机后,我调用了以下函数。这需要记录所有数据和采样率。就我而言,它是 44100。

await save(recordedData, 44100);

Future<void> save(List<int> data, int sampleRate) async {
    File recordedFile = File("/storage/emulated/0/recordedFile.wav");

    var channels = 1;

    int byteRate = ((16 * sampleRate * channels) / 8).round();

    var size = data.length;

    var fileSize = size + 36;

    Uint8List header = Uint8List.fromList([
      // "RIFF"
      82, 73, 70, 70,
      fileSize & 0xff,
      (fileSize >> 8) & 0xff,
      (fileSize >> 16) & 0xff,
      (fileSize >> 24) & 0xff,
      // WAVE
      87, 65, 86, 69,
      // fmt
      102, 109, 116, 32,
      // fmt chunk size 16
      16, 0, 0, 0,
      // Type of format
      1, 0,
      // One channel
      channels, 0,
      // Sample rate
      sampleRate & 0xff,
      (sampleRate >> 8) & 0xff,
      (sampleRate >> 16) & 0xff,
      (sampleRate >> 24) & 0xff,
      // Byte rate
      byteRate & 0xff,
      (byteRate >> 8) & 0xff,
      (byteRate >> 16) & 0xff,
      (byteRate >> 24) & 0xff,
      // Uhm
      ((16 * channels) / 8).round(), 0,
      // bitsize
      16, 0,
      // "data"
      100, 97, 116, 97,
      size & 0xff,
      (size >> 8) & 0xff,
      (size >> 16) & 0xff,
      (size >> 24) & 0xff,
      ...data
    ]);
    return recordedFile.writeAsBytesSync(header, flush: true);
  }

它正在创建可播放的 WAV 文件。

【讨论】:

  • 当我执行await save(_micChunks, 44100); 时,我得到The argument type List&lt;Uint8List&gt; can't be assigned to List&lt;int&gt;。我正在使用插件 sound_stream
  • Uint8List 仅扩展到 List。所以在你的情况下,你有 List 尝试使用 List>
  • 如果我将 List&lt;int&gt; recordedData = []; 更改为 List&lt;List&lt;int&gt;&gt; data = _micChunks; 标题 ...data 上的最后一段代码,请说 List&lt;int&gt; cannot be assigned to int
  • 您能否分享您使用的软件包或插件的任何链接,以便我可以相应地指导您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多