簡體   English   中英

在文件 flutter 中保存音頻 stream / Uint8List 數據

[英]Save audio stream / Uint8List data in file flutter

我從 Android 和Uint8List的記錄器插件中獲取 Uint8List 。 每當我在麥克風的 stream 訂閱中獲取數據時,我想將數據寫入本地可播放的音頻文件。 有沒有可能的方法來寫入數據?

目前,我正在存儲數據,例如

recordedFile.writeAsBytesSync(recordedData, flush: true);

它正在將數據寫入文件,但無法從文件存儲中播放。 但是,如果我讀取相同的文件並將其字節提供給插件,則它正在播放相同的緩沖區。

在將我的字節寫入文件之前,我添加了一個 WAVE/RIFF Header,它將元數據提供給字節和文件。

我的 stream 中有 16 位 PCM 音頻字節和 MONO 通道。 當我接收字節時,我將這些字節附加到字節列表中。

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

現在我的列表中有所有記錄的字節。 停止我的錄音機后,我調用了下面的 function。 這需要記錄所有數據和采樣率。 就我而言,它是 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 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM