llfbandit / record

Audio recorder from microphone to a given file path. No external dependencies, MediaRecorder is used for Android an AVAudioRecorder for iOS.
https://pub.dev/packages/record
241 stars 202 forks source link

[Android] App Crashes consistently when calling `stop()` #381

Closed nirmalatgallabox closed 2 months ago

nirmalatgallabox commented 3 months ago

Package version 5.0.4 and above

Environment

Describe the bug

App crashes consistently when AudioRecorder().stop() method is called. This crash happens on Android 9 emulator and some of the real devices including

RecordConfig(encoder: AudioEncoder.aacLc, bitRate: 128000)

To Reproduce

Steps to reproduce the behavior:

  1. Start the audio recorder by calling start()
  2. Stop the audio recorder after few seconds by calling stop()

Expected behavior

Calling stop() Should stop recording and provide the recorded audio file path

Additional context

Please find the complete log in the below file which was recorded during crash.

audio_crash_log_report.txt

llfbandit commented 3 months ago

This should be already fixed in record_android: 1.2.5 Can you confirm that you were not in a lower version?

nirmalatgallabox commented 3 months ago

I just tried record_android: 1.2.5,

Now app is not crashing but getting the below exception from native platform, this happens when stop() is called

audio_crash_report_2.txt

nirmalatgallabox commented 2 months ago

Now we are got some more crashes Crashed Devices

@llfbandit Do you have any update on this?

llfbandit commented 2 months ago

Yes :) Are you able to check the modifications from the repository before release? FYI, I didn't reproduced the issue with my device, so...

nirmalatgallabox commented 2 months ago

Sure, will check and let you know.

You can actually reproduce the issue, if you run on Android 9 Simulator

llfbandit commented 2 months ago

Well I don't rely on simulators in audio recording area. Too much inconsistencies.

nirmalatgallabox commented 2 months ago

Now got a new error, this happens after start() is called

E/AudioRecorder(31752): Unable to instantiate PCM reader.
E/AudioRecorder(31752): java.lang.Exception: Unable to instantiate PCM reader.
E/AudioRecorder(31752):         at com.llfbandit.record.record.PCMReader.createReader(PCMReader.kt:101)
E/AudioRecorder(31752):         at com.llfbandit.record.record.PCMReader.<init>(PCMReader.kt:28)
E/AudioRecorder(31752):         at com.llfbandit.record.record.recorder.RecordThread.startRecording$lambda$0(RecordThread.kt:92)
E/AudioRecorder(31752):         at com.llfbandit.record.record.recorder.RecordThread.$r8$lambda$ldmev7sgpiaTAinIeDKedqNmCzU(Unknown Source:0)
E/AudioRecorder(31752):         at com.llfbandit.record.record.recorder.RecordThread$$ExternalSyntheticLambda0.run(Unknown Source:2)
E/AudioRecorder(31752):         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AudioRecorder(31752):         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AudioRecorder(31752):         at java.lang.Thread.run(Thread.java:764)
E/AudioRecorder(31752): Caused by: java.lang.IllegalArgumentException: Invalid audio buffer size 6989 (frame size 4)
E/AudioRecorder(31752):         at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:731)
E/AudioRecorder(31752):         at android.media.AudioRecord.<init>(AudioRecord.java:380)
E/AudioRecorder(31752):         at android.media.AudioRecord.<init>(AudioRecord.java:284)
E/AudioRecorder(31752):         at com.llfbandit.record.record.PCMReader.createReader(PCMReader.kt:93)
E/AudioRecorder(31752):         ... 7 more

Tested Devices

llfbandit commented 2 months ago

Argh, this has nothing to do with the current issue. You can pull again, I propagated another fix for this.

nirmalatgallabox commented 2 months ago

@llfbandit

Got this exception after pull

D/MPEG4Writer( 8964): Audio track stopping. Stop source
D/MPEG4Writer( 8964): Audio track source stopping
D/MPEG4Writer( 8964): Audio track source stopped
E/MPEG4Writer( 8964): Missing codec specific data
E/MPEG4Writer( 8964): Dumping Audio track's last 10 frames timestamp and frame type 
E/MPEG4Writer( 8964): 
I/MPEG4Writer( 8964): Received total/0-length (150/1) buffers and encoded 150 frames. - Audio
I/MPEG4Writer( 8964): Audio track drift time: 0 us
D/MPEG4Writer( 8964): Audio track stopped. Stop source
D/MPEG4Writer( 8964): Stopping writer thread
D/MPEG4Writer( 8964): 0 chunks are written in the last batch
D/MPEG4Writer( 8964): Writer thread stopped
E/AudioRecorder( 8964): Failed to stop the muxer
E/AudioRecorder( 8964): java.lang.IllegalStateException: Failed to stop the muxer
E/AudioRecorder( 8964):         at android.media.MediaMuxer.nativeStop(Native Method)
E/AudioRecorder( 8964):         at android.media.MediaMuxer.stop(MediaMuxer.java:454)
E/AudioRecorder( 8964):         at com.llfbandit.record.record.container.MuxerContainer.stop(MuxerContainer.kt:30)
E/AudioRecorder( 8964):         at com.llfbandit.record.record.encoder.MediaCodecEncoder.stopAndRelease(MediaCodecEncoder.kt:198)
E/AudioRecorder( 8964):         at com.llfbandit.record.record.encoder.MediaCodecEncoder.finish(MediaCodecEncoder.kt:185)
E/AudioRecorder( 8964):         at com.llfbandit.record.record.encoder.MediaCodecEncoder.processOutputBuffer(MediaCodecEncoder.kt:171)
E/AudioRecorder( 8964):         at com.llfbandit.record.record.encoder.MediaCodecEncoder.access$processOutputBuffer(MediaCodecEncoder.kt:16)
E/AudioRecorder( 8964):         at com.llfbandit.record.record.encoder.MediaCodecEncoder$AudioRecorderCodecCallback.onOutputBufferAvailable(MediaCodecEncoder.kt:225)
E/AudioRecorder( 8964):         at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1699)
E/AudioRecorder( 8964):         at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1646)
E/AudioRecorder( 8964):         at android.os.Handler.dispatchMessage(Handler.java:106)
E/AudioRecorder( 8964):         at android.os.Looper.loop(Looper.java:193)
E/AudioRecorder( 8964):         at android.os.HandlerThread.run(HandlerThread.java:65)

Tested Devices

llfbandit commented 2 months ago

I guess I finally found the bug. You can pull latests commits.

I also tested on Emulator with Android 9 where I reproduced the issue too.

nirmalatgallabox commented 2 months ago

Yeah, Now it is working fine, thanks for your update :)

llfbandit commented 2 months ago

Released in record_android 1.2.6