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.
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},");
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.
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,
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With