This error happens to me even if I make toFile: 'flutter_example.aac' as in the package example ..
here is my log:
I/flutter (15733): ---> openRecorderCompleted: true
I/flutter (15733): <--- openRecorderCompleted: true
I/flutter (15733): <--- openAudioSession
I/flutter (15733): FS:<--- openAudioSession
I/flutter (15733): ********* record outputpath : /data/user/0/com.q8intouch.hareem/cache/c34s2.aac
I/flutter (15733): FS:---> startRecorder
I/flutter (15733): FS:---> _startRecorder.
I/flutter (15733): Calling instance.startRecorder
E/MediaRecorder(15733): start failed: -38
E/SoundMediaRecorder(15733): Exception:
E/SoundMediaRecorder(15733): java.lang.IllegalStateException
E/SoundMediaRecorder(15733): at android.media.MediaRecorder.start(Native Method)
E/SoundMediaRecorder(15733): at com.dooboolab.TauEngine.q.a(:182)
E/SoundMediaRecorder(15733): at com.dooboolab.TauEngine.m.r(:239)
E/SoundMediaRecorder(15733): at f.e.b.e.M(:259)
E/SoundMediaRecorder(15733): at f.e.b.f.onMethodCall(:118)
E/SoundMediaRecorder(15733): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(:233)
E/SoundMediaRecorder(15733): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(:85)
E/SoundMediaRecorder(15733): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(:818)
E/SoundMediaRecorder(15733): at android.os.MessageQueue.nativePollOnce(Native Method)
E/SoundMediaRecorder(15733): at android.os.MessageQueue.next(MessageQueue.java:325)
E/SoundMediaRecorder(15733): at android.os.Looper.loop(Looper.java:142)
E/SoundMediaRecorder(15733): at android.app.ActivityThread.main(ActivityThread.java:6494)
E/SoundMediaRecorder(15733): at java.lang.reflect.Method.invoke(Native Method)
E/SoundMediaRecorder(15733): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E/SoundMediaRecorder(15733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
E/MediaRecorder(15733): resume called in an invalid state: 0
E/MediaRecorder(15733): stop called in an invalid state: 0
D/SoundMediaRecorder(15733): Error Stop Recorder
E/FlautoRecorder(15733): Error starting recordernull
E/flutter (15733): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(startRecorder, startRecorder, Failure to start recorder, null)
E/flutter (15733): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:597
E/flutter (15733): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:158
E/flutter (15733): <asynchronous suspension>
E/flutter (15733): #2 FlutterSoundRecorder._startRecorder
package:flutter_sound/public/flutter_sound_recorder.dart:662
E/flutter (15733): <asynchronous suspension>
E/flutter (15733): #3 FlutterSoundRecorder.startRecorder.<anonymous closure>
package:flutter_sound/public/flutter_sound_recorder.dart:584
E/flutter (15733): <asynchronous suspension>
E/flutter (15733): #4 BasicLock.synchronized
package:synchronized/src/basic_lock.dart:33
E/flutter (15733): <asynchronous suspension>
E/flutter (15733): #5 FlutterSoundRecorder.startRecorder
package:flutter_sound/public/flutter_sound_recorder.dart:583
E/flutter (15733): <asynchronous suspension>
Can you provide the exact code you're using to open the audio session and start the recorder? Also can you provide the output of flutter pub deps | grep flutter_sound?
What are your parameters to openAudioSession() and startRecorder() ?
functions:
import 'dart:io';
import 'dart:math';
import 'package:flutter_sound/public/flutter_sound_recorder.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:path_provider/path_provider.dart' as pathProvider;
class SoundRecorder {
FlutterSoundRecorder _audioRecorder;
bool _isRecorderInitialized = false;
bool get isRecording => _audioRecorder.isRecording;
String _chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
Future init() async {
_audioRecorder = FlutterSoundRecorder();
final status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
throw RecordingPermissionException(
'Microphone permission is not granted');
}
await _audioRecorder.openAudioSession();
_isRecorderInitialized = true;
}
Future dispose() async {
_audioRecorder = FlutterSoundRecorder();
await _audioRecorder.closeAudioSession();
_isRecorderInitialized = false;
}
Future _record() async {
if (!_isRecorderInitialized) return;
Directory tempDir = await pathProvider.getTemporaryDirectory();
String outputPath = '${tempDir.path}/${getRandomString(5)}.aac';
print('********* record outputpath : $outputPath');
await _audioRecorder.startRecorder(toFile: outputPath);
}
Future _stop() async {
if (!_isRecorderInitialized) return;
await _audioRecorder.stopRecorder();
_audioRecorder = null;
}
Future toggleRecorder() async {
if (_audioRecorder.isStopped) {
await _record();
} else {
await _stop();
}
}
}
widget:
class _VoiceoverPageState extends State<VoiceoverPage> {
final recorder = SoundRecorder();
@override
void initState() {
super.initState();
recorder.init();
}
@override
void dispose() {
recorder.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
return Scaffold(
body: Column(children: [buildRecordingButton()]),);
}
Widget buildRecordingButton() {
final isRecording = recorder.isRecording;
return GestureDetector(
onTap: () async {
await recorder.toggleRecorder();
setState(() {});
},
child: Container(
width: 75,
height: 75,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: Center(
child: Container(
margin: EdgeInsets.all(25),
decoration: BoxDecoration(
shape: isRecording ? BoxShape.rectangle : BoxShape.circle,
color: Colors.red,
),
)),
),
);
}}
this is flutter pub deps | grep flutter_sound
flutter_sound 8.1.9
| |-- flutter...
| |-- flutter_sound_platform_interface 8.1.9
| | |-- flutter...
| | |-- meta...
| | '-- plugin_platform_interface...
| |-- flutter_sound_web 8.1.9
| | |-- flutter...
| | |-- flutter_sound_platform_interface...
| | |-- flutter_web_plugins...
| | |-- js...
| | '-- meta...
| |-- flutter_spinkit...
| |-- logger 1.0.0
| |-- path...
| |-- path_provider...
| |-- provider...
| |-- recase 4.0.0
| |-- synchronized 3.0.0
| '-- uuid...
I do not see any problem in your code.
Maybe you can try to specify the Codec AAC in start recorder : the recorder do not use the file extension (xxx.aac).
But I would be surprised if this solve your problem : AAC is the default codec, I think.
@mhstoller : do you see any problem in Abdallah's code ?
No, actually I've run it on my device and it gets past the startRecorder portion on my phone.
What device and android version are you using for development @AbdallahLabib ?
I/flutter ( 5167): ********* record outputpath : /data/user/0/com.example.recorder_problem/cache/aIy3i.aac
I/flutter ( 5167): FS:---> startRecorder
I/flutter ( 5167): FS:---> _startRecorder.
I/flutter ( 5167): Calling instance.startRecorder
I/flutter ( 5167): ---> startRecorderCompleted: true
I/flutter ( 5167): <--- startRecorderCompleted: true
I/flutter ( 5167): FS:<--- _startRecorder.
I/flutter ( 5167): FS:<--- startRecorder
2
I/flutter ( 5167): FS:---> stopRecorder
I/flutter ( 5167): FS:---> stop
I/flutter ( 5167): ---> stopRecorderCompleted: true
I/flutter ( 5167): <---- stopRecorderCompleted: true
I/flutter ( 5167): FS:<--- stop
I/flutter ( 5167): FS:<--- stopRecorder : /data/user/0/com.example.recorder_problem/cache/aIy3i.aac
Thanks guys, the problem was from the emulator .. I can't record from it .. when I tried it from phone it worked very well .. Thanks for your fast reply
Good to hear that you solved your problems.
Thanks to @mhstoller who spent time on this 💯
Most helpful comment
No, actually I've run it on my device and it gets past the startRecorder portion on my phone.
What device and android version are you using for development @AbdallahLabib ?