Flutter — Как сохранить записанный аудиофайл?

#flutter #file #dart #audio

Вопрос:

Я использую flutter_sound для записи голоса(аудио).

Я не вижу сохранения файла в эмуляторе после остановки записи.

Класс рекордера:

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

final _pathToAudio = 'myFile.aac';

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

  Future init() async {
    _recorder = FlutterSoundRecorder();

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

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

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

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

  Future stop() async {
    if (!_isRecorderInitialized) {
      return;
     }
    await _recorder!.stopRecorder();  
    print('stopped....');   //I GET THIS MESSAGE SO I AM GUESSING THE RECORDING IS HAPPENING BUT CANT FIND THE FILE LATER IN THE EMULATOR.
  }

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

Функция при нажатии кнопки:

 void startStopRecording() async {
   final isRecordingOn = await recorder.toggleRecording();
     setState(() {
     _isRecording = recorder.isRecording;
 });
  if(_isRecording) {
   startTimer();
   _stopwatch.start();
  
 } else {
  stopTimer();
  _stopwatch.stop();
 
 }
}