使用 Flutter 录音

Voice recording using Flutter

我正在尝试使用 Flutter 构建一个应用程序,其中包含录音机。如何访问录音机?请帮我找出它的包、依赖项和代码。

您可以使用 audio_recorder 包:

https://pub.dev/packages/audio_recorder

// Import package
import 'package:audio_recorder/audio_recorder.dart';

// Check permissions before starting
bool hasPermissions = await AudioRecorder.hasPermissions;

// Get the state of the recorder
bool isRecording = await AudioRecorder.isRecording;

// Start recording
await AudioRecorder.start(path: _controller.text, audioOutputFormat: AudioOutputFormat.AAC);

// Stop recording
Recording recording = await AudioRecorder.stop();
print("Path : ${recording.path},  Format : ${recording.audioOutputFormat},  Duration : ${recording.duration},  Extension : ${recording.extension},");

另一种有效的方法是使用 https://pub.dev/packages/flutter_sound 包。

来自文档的用法示例:

Future<String> result = await flutterSound.startRecorder(codec: t_CODEC.CODEC_AAC,);

result.then(path) {
    print('startRecorder: $path');

    _recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
    DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
    String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
    });
}

显然,目前只有 AAC 编解码器是录制的有效选择。至少,其他选项对我不起作用。

这是我的代码,它与 Whatsapp 录音的工作方式相同。当您长按麦克风图标开始录音时,该图标可以拖动到另一个图标,如删除。 我使用了“path_provider”、“record”和“audioplayers”库。

       Material(
        child: Container(
          padding: EdgeInsets.only(top: 0, bottom: 12, left: 5, right: 5),
          child: LongPressDraggable(
            child: Icon(
              Icons.mic,
              color: Colors.black54,
            ),
            onDragStarted: () async {
              stopWatchTimer.onExecute.add(StopWatchExecute.reset);
              stopWatchTimer.onExecute.add(StopWatchExecute.start);
              seconds = 0;
              minutes = 0;
              setState(() {
                showMic = true;
              });
              isRecording = true;
              bool result = await Record().hasPermission();
              if (result) {
                Directory tempDir = await getTemporaryDirectory();
                File filePath = File('${tempDir.path}/audio.mp3');
                audioPath = filePath.path;
                await Record().start(
                  path: filePath.path, // required
                  encoder: AudioEncoder.AAC, // by default
                  bitRate: 128000, // by default
                  samplingRate: 44100, // by default
                );
              }
            },
            onDragEnd: (a) async {
              if(a.offset.dx<width-width/5){
                  await Record().stop();
                  AudioPlayer audioPlayer = AudioPlayer(
                    mode: PlayerMode.MEDIA_PLAYER,
                  );
                  int result = await audioPlayer.play(audioPath,
                      isLocal: true, stayAwake: true);
                  stopWatchTimer.onExecute.add(StopWatchExecute.stop);
                  setState(() {
                    showMic = false;
                    isRecording = false;
                  });

              }else{
                setState(() {
                  showMic = false;
                  isRecording = false;
                });
                StaticDataAndFunctions.customToastMessage(
                    "Record removed!");
              }

            },
            feedback: Icon(
              Icons.delete,
              color: Colors.black54,
            ),
          ),
        ),
        color: Colors.white,
      ),