【问题标题】:Flutter - How to save and play a recorded audio file?Flutter - 如何保存和播放录制的音频文件?
【发布时间】:2021-10-08 14:42:00
【问题描述】:

我这辈子都想不通。我要做的就是录制音频(如在录音机中),然后才能播放。

录音机类:

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'(操作系统错误:没有这样的文件或目录,errno = 2)”。

我正在使用flutter_sound

【问题讨论】:

  • 你检查文件是否存在?
  • 点赞100次

标签: flutter dart audio filesystems


【解决方案1】:

尝试使用path_provider 初始化您的文件路径。

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

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

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

【讨论】:

    【解决方案2】:
    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();
    }
    

    【讨论】:

    • 这适用于 Android 10+ 设备吗?
    • 什么是 generateFileName() ..?它是在哪里定义的??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多