Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Voice recording using Flutter

I am trying to build an app using Flutter which will have voice recorder in it. How do I access voice recorder? Please help me figure out the Packages, Dependencies and Code for it.

like image 361
Devansh Baldwa Avatar asked Oct 20 '19 09:10

Devansh Baldwa


3 Answers

You can use the audio_recorder package:

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},");
like image 160
Florin Bratan Avatar answered Oct 17 '22 13:10

Florin Bratan


Another valid approach is to use https://pub.dev/packages/flutter_sound package.

Usage example from docs:

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);
    });
}

Apparently, only AAC codec is valid choice for the recording for now. At least, other options didn't work for me.

like image 30
beyondfloatingpoint Avatar answered Oct 17 '22 11:10

beyondfloatingpoint


This is my code it is same way Whatsapp recordings work. when you long press on mic icon start recording and the icon able to drag to another icon like delete. I have used "path_provider" , "record", and "audioplayers" libraries.

       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,
      ),
like image 21
Feisal Aswad Avatar answered Oct 17 '22 13:10

Feisal Aswad