【问题标题】:Flutter - How to save audio as a file?Flutter - 如何将音频保存为文件?
【发布时间】:2022-10-01 13:47:29
【问题描述】:

我在用 :

  • flutter_sound_lite 录制一些音频。
  • 和 path_provider 来获取我手机的路径。
  • permission_handler

我正在创建目录和具有指定路径的文件来放置我录制的音频。

我想知道我是没有找到它还是它没有被创建。

import \'dart:io\';
import \'dart:typed_data\';

import \'package:flutter_sound_lite/flutter_sound.dart\';
import \'package:flutter_sound_lite/public/flutter_sound_recorder.dart\';
import \'package:path_provider/path_provider.dart\';
import \'package:permission_handler/permission_handler.dart\';


class SoundRecorder {
  FlutterSoundRecorder? _audioRecorder;
  ModelApiShazam? modelApiShazam;

  bool _isRecorderInitialised = false;

  bool get isRecording => _audioRecorder!.isRecording;

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

    final statusMic = await Permission.microphone.request();
    if(statusMic != PermissionStatus.granted){
      throw RecordingPermissionException(\'microphone permission\');
    }
    final statusStorage = await Permission.storage.status;
    if (!statusStorage.isGranted) {
      await Permission.storage.request();
    }
    await _audioRecorder!.openAudioSession();
    directoryPath = await _directoryPath();
    completePath = await _completePath(directoryPath);
    _createDirectory();
    _createFile();
    _isRecorderInitialised = true;
  }

  void dispose(){
    if(!_isRecorderInitialised) return;

    _audioRecorder!.closeAudioSession();
    _audioRecorder = null;
    _isRecorderInitialised = false;
  }

  Future _record() async{
    if(!_isRecorderInitialised) return;
    print(\"Path where the file will be : \"+completePath);
    await _audioRecorder!.startRecorder(
        toFile: completePath,
        numChannels : 1,
        sampleRate: 44100,
    );
  }

  Future _stop() async{
    if(!_isRecorderInitialised) return;
    var s = await _audioRecorder!.stopRecorder();
    File f = File(completePath);
    print(\"The created file : $f\");
  }

  Future toggleRecording() async{
    if(_audioRecorder!.isStopped){
      await _record();
    }else{
      await _stop();

    }
  }

  String completePath = \"\";
  String directoryPath = \"\";

  Future<String> _completePath(String directory) async {
    var fileName = _fileName();
    return \"$directory$fileName\";
  }

  Future<String> _directoryPath() async {
    var directory = await getApplicationDocumentsDirectory();
    var directoryPath = directory.path;
    return \"$directoryPath/records/\";
  }

  String _fileName() {
    return \"record.wav\";
  }


  Future _createFile() async {
    File(completePath)
        .create(recursive: true)
        .then((File file) async {
      //write to file
      Uint8List bytes = await file.readAsBytes();
      file.writeAsBytes(bytes);
      print(\"FILE CREATED AT : \"+file.path);
    });
  }

  void _createDirectory() async {
    bool isDirectoryCreated = await Directory(directoryPath).exists();
    if (!isDirectoryCreated) {
      Directory(directoryPath).create()
          .then((Directory directory) {
        print(\"DIRECTORY CREATED AT : \" +directory.path);
      });
    }
  }


}

输出不包括 flutter_sound :

I/flutter (20652): DIRECTORY CREATED AT : /data/user/0/com.example.shazam/app_flutter/records/
I/flutter (20652): FILE CREATED AT : /data/user/0/com.example.shazam/app_flutter/records/record.wav

我按下按钮开始录制...

I/flutter (20652): Path where the file will be : /data/user/0/com.example.shazam/app_flutter/records/record.wav

我按下按钮结束记录...

I/flutter (20652): The created file : File: \'/data/user/0/com.example.shazam/app_flutter/records/record.wav\'

即使我遵循路径,我也找不到此文件的位置

    标签: android flutter dart audio


    【解决方案1】:

    我找到了解决方案!

    只需更换

        var directory = await getApplicationDocumentsDirectory();
        var directoryPath = directory.path;
    

    经过

        var directory = await getExternalStorageDirectory();
        var directoryPath = directory!.path;
    

    【讨论】:

      【解决方案2】:

      getApplicationDocumentsDirectory() 或 getExternalStorageDirectory() 的代码是什么?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多