title: Flutter - How to save and play a recorded audio file?
body: <p>I, for the life of me, can't figure this out. All I am trying to do is record an audio (as in a sound/voice recorder) and later be able to play it.</p>
  <p>Recorder class:</p>
  <pre><code>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 = &quot;/storage/emulated/0&quot;;
  
  class Recorder {
    FlutterSoundRecorder? _recorder;
    bool _isRecorderInitialized = false;
    bool get isRecording =&gt; _recorder!.isRecording;
  
    Future init() async {
      _recorder = FlutterSoundRecorder();
      //final directory = &quot;/sdcard/downloads/&quot;;
      //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();
      }
    }
  }
  </code></pre>
  <p>Currently the error I am getting is &quot;Cannot open file, path = '/storage/emulated/0/myAudio.aac' (OS Error: No such file or directory, errno = 2)&quot;.</p>
  <p>I am using <a href="https://pub.dev/packages/flutter_sound" rel="nofollow noreferrer">flutter_sound</a></p>