Flutter - 如何保存和播放录制的音频文件?

Flutter - How to save and play a recorded audio file?

我这辈子都想不通。我想做的就是录制一段音频(如在 sound/voice 录音机中),然后再播放它。

录音机class:

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

//String _pathToAudio = '/sdcard/myAudio.aac';
String _fileName = 'myAudio.aac';
String _path = "/storage/emulated/0";

class Recorder {
  FlutterSoundRecorder? _recorder;
  bool _isRecorderInitialized = false;
  bool get isRecording => _recorder!.isRecording;

  Future init() async {
    _recorder = FlutterSoundRecorder();
    //final directory = "/sdcard/downloads/";
    //Directory? extStorageDir = await getExternalStorageDirectory();
    //String _path = directory.path;

    final status = await Permission.microphone.request();
    if (status != PermissionStatus.granted) {
      throw RecordingPermissionException('Recording permission required.');
    }

    await _recorder!.openAudioSession();
    _isRecorderInitialized = true;
  }

  void _writeFileToStorage() async {
    File audiofile = File('$_path/$_fileName');
    Uint8List bytes = await audiofile.readAsBytes();
    audiofile.writeAsBytes(bytes);
  }

  void dispose() {
    _recorder!.closeAudioSession();
    _recorder = null;
    _isRecorderInitialized = false;
  }

  Future record() async {
    if (!_isRecorderInitialized) {
      return;
    }
    print('recording....');
    await _recorder!.startRecorder(
      toFile: '$_fileName',
      //codec: Codec.aacMP4,
    );
  }

  Future stop() async {
    if (!_isRecorderInitialized) {
      return;
    }
    await _recorder!.stopRecorder();
    _writeFileToStorage();
    print('stopped....');
  }

  Future toggleRecording() async {
    if (_recorder!.isStopped) {
      await record();
    } else {
      await stop();
    }
  }
}

目前我收到的错误是“无法打开文件,路径 = '/storage/emulated/0/myAudio.aac'(OS 错误:没有这样的文件或目录,errno = 2)”。

我正在使用flutter_sound

尝试使用 path_provider.

初始化您的文件路径

将这两行添加到 init 函数的开头。

    final directory = await getApplicationDocumentsDirectory();
    _path = directory.path; // instead of "/storage/emulated/0"

不确定您是如何尝试访问和播放该文件的,但在我看来,它至少清除了错误。

String _fileName = 'Recording_';
String _fileExtension = '.aac';
String _directoryPath = '/storage/emulated/0/SoundRecorder';

这就是我目前拥有的并且正在运行。

void _createFile() async {
  var _completeFileName = await generateFileName();
  File(_directoryPath + '/' + _completeFileName)
      .create(recursive: true)
      .then((File file) async {
    //write to file
    Uint8List bytes = await file.readAsBytes();
    file.writeAsBytes(bytes);
    print(file.path);
  });
}

void _createDirectory() async {
  bool isDirectoryCreated = await Directory(_directoryPath).exists();
  if (!isDirectoryCreated) {
    Directory(_directoryPath).create()
        // The created directory is returned as a Future.
        .then((Directory directory) {
      print(directory.path);
    });
  }
}

void _writeFileToStorage() async {
  _createDirectory();
  _createFile();
}