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

Empty audio created on Android in some cases #271

Closed akshatbaranwal closed 5 months ago

akshatbaranwal commented 10 months ago

Package version 5.0.4

Environment Android

Describe the bug

On certain Android devices, the audio file created is of 0 bytes. No such issues on version 4.4.4 RecordConfig( echoCancel: false, noiseSuppress: false, autoGain: false, numChannels: 1, encoder: AudioEncoder.aacLc, sampleRate: 44100, )

To Reproduce I could not reproduce it as the issue didn't occur on any of my devices. It happened on the client's device. I released a new version of the app with 4.4.4 and it started working for everyone who had been facing this issue.

ProFive commented 10 months ago

![Uploading image_2024_01_16T07_28_06_824Z.png…]() The same issue

llfbandit commented 10 months ago

Well, of course I can't reproduce this...

@ProFive Are you able to reproduce it with a device that you own? If so, is this reproducible with the example project to get away from other unknown factors? Is there any log that would be useful in this case?

MilosKarakas commented 10 months ago

@llfbandit Hi, here is an example of Android logs that might be related to this case. Audio is recorded using record v5.0.4 on a Huawei device Huawei P40 Lite with EMUI 12.0.4 - it should be based on Android 10 I believe. After that it's being played with AudioPlayer

Audio sink error E/MediaCodecAudioRenderer( 5425): com.google.android.exoplayer2.audio.AudioSink$UnexpectedDiscontinuityException: Unexpected audio track timestamp discontinuity: expected 1000000232199, got 1000000023219 E/MediaCodecAudioRenderer( 5425): at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:999) E/MediaCodecAudioRenderer( 5425): at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:709) E/MediaCodecAudioRenderer( 5425): at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1889) E/MediaCodecAudioRenderer( 5425): at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:782) E/MediaCodecAudioRenderer( 5425): at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:1007) E/MediaCodecAudioRenderer( 5425): at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:502) E/MediaCodecAudioRenderer( 5425): at android.os.Handler.dispatchMessage(Handler.java:106) E/MediaCodecAudioRenderer( 5425): at android.os.Looper.loop(Looper.java:219) E/MediaCodecAudioRenderer( 5425): at android.os.HandlerThread.run(HandlerThread.java:67) E/MediaCodecAudioRenderer( 5425): Audio sink error

alpaca211 commented 8 months ago

I'm experiencing the same issue on older Android devices (API 29). It did not occur with version 4.4.4 but does with version 5.0.4. Interestingly, the issue does not appear on API 34 either. Here's the RecordConfig I'm using:

RecordConfig( encoder: AudioEncoder.aacLc, bitRate: 128000, sampleRate: 44100, numChannels: 1, );

When audio recorded with this configuration is played using the Just Audio plugin with player.setAudioSource, the error occurs.

llfbandit commented 7 months ago

Is it better with version 5.0.5 and record_android 1.1.0?

alpaca211 commented 7 months ago

Device: Oppo Reno A Android Version: 9 record version: 5.0.5 record_android version: 1.1.0

I'm not sure if this is related to the issue I'm experiencing, but when I attempt to start recording in the record example project under the above conditions, it crashes.

The line where it crashes: await recordFile(_audioRecorder, config);

Error message: I/.record_exampl(25781): ProcessProfilingInfo new_methods=1785 is saved saved_to_disk=1 resolve_classes_delay=5000 W/AudioCapabilities(25781): Unsupported mime audio/ffmpeg W/VideoCapabilities(25781): Unsupported mime video/ffmpeg W/VideoCapabilities(25781): Unsupported mime video/divx W/VideoCapabilities(25781): Unsupported mime video/divx4 W/VideoCapabilities(25781): Unrecognized profile 4 for video/hevc W/VideoCapabilities(25781): Unrecognized profile/level 0/3 for video/mpeg2 W/VideoCapabilities(25781): Unrecognized profile/level 0/3 for video/mpeg2 W/VideoCapabilities(25781): Unsupported mime video/x-ms-wmv I/VideoCapabilities(25781): Unsupported profile 4 for video/mp4v-es I/flutter (25781): [ id: 14 I/flutter (25781): label: CPH1983 (built-in microphone, bottom) I/flutter (25781): , id: 15 I/flutter (25781): label: CPH1983 (built-in microphone, back) I/flutter (25781): , id: 11 I/flutter (25781): label: CPH1983 (FM tuner, ) I/flutter (25781): ] D/IAtlas (25781): IAtlas::init CallingPid 25781 D/IAtlas (25781): IAtlas::init this 0x6fc7945f00 D/AudioRecord(25781): set() inputSource 0, sampleRate 44100, format 0x1, channelMask 0x10, frameCount 7168, notificationFrames 0, sessionId 0, transferType 0, flags 0, opPackageName com.llfbandit.record_example uid -1, pid -1 I/MediaCodec(25781): CreateByComponentName(), name = OMX.google.aac.encoder I/MediaCodec(25781): new MediaCodec() I/ACodec (25781): new ACodec() I/ACodec (25781): onAllocateComponent() I/OMXClient(25781): IOmx service obtained I/ACodec (25781): Successfully allocate component [OMX.google.aac.encoder] I/MediaCodec(25781): configure format is:{sample-rate=44100, mime=audio/mp4a-latm, channel-count=1, bitrate=128000, aac-profile=2} I/MediaCodec(25781): configure() I/ACodec (25781): configureCodec() mime = audio/mp4a-latm W/ExtendedACodec(25781): Failed to get extension for extradata parameter D/AudioRecord(25781): start() sync event 0 trigger session 0 D/AudioRecord(25781): 2024-03-28 18:27:26 [record][com.llfbandit.record_example]:[AudioRecord::start()] LocalRecording [44100,1,0x1] I/MediaCodec(25781): MediaCodec will operate in async mode I/MediaCodec(25781): Audio start() E/Utils (25781): csd0 too small E/ExtendedUtils(25781): csd0 too small I/MPEG4Writer(25781): start() I/MPEG4Writer(25781): limits: 4294967295/0 bytes/us, bit rate: -1 bps and the estimated moov size 3191 bytes I/MPEG4Writer(25781): start() Audio trck I/MPEG4Writer(25781): setStartTimestampUs: 0 I/MPEG4Writer(25781): Earliest track starting time: 0 W/MPEG4Writer(25781): 0-duration samples found: 1 E/MPEG4Writer(25781): do not support out of order frames (timestamp: 0 < last: 23219 for Audio track E/MPEG4Writer(25781): Dumping Audio track's last 10 frames timestamp and frame type E/MPEG4Writer(25781): I/MPEG4Writer(25781): Received total/0-length (4/0) buffers and encoded 3 frames. - Audio I/MPEG4Writer(25781): Audio track drift time: 0 us E/MediaAdapter(25781): pushBuffer called before start E/AudioRecorder(25781): writeSampleData returned an error E/AudioRecorder(25781): java.lang.IllegalStateException: writeSampleData returned an error E/AudioRecorder(25781): at android.media.MediaMuxer.nativeWriteSampleData(Native Method) E/AudioRecorder(25781): at android.media.MediaMuxer.writeSampleData(MediaMuxer.java:682) E/AudioRecorder(25781): at com.llfbandit.record.record.container.MuxerContainer.writeSampleData(MuxerContainer.kt:29) E/AudioRecorder(25781): at com.llfbandit.record.record.encoder.MediaCodecEncoder.onOutputBufferAvailable(MediaCodecEncoder.kt:132) E/AudioRecorder(25781): at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1703) E/AudioRecorder(25781): at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1650) E/AudioRecorder(25781): at android.os.Handler.dispatchMessage(Handler.java:106) E/AudioRecorder(25781): at android.os.Looper.loop(Looper.java:227) E/AudioRecorder(25781): at android.app.ActivityThread.main(ActivityThread.java:7275) E/AudioRecorder(25781): at java.lang.reflect.Method.invoke(Native Method) E/AudioRecorder(25781): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497) E/AudioRecorder(25781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908) I/MediaCodec(25781): stop() I/MediaCodec(25781): Audio release() I/MediaCodec(25781): ~MediaCodec() W/MediaAnalyticsItem(25781): Unable to record: [1:codec:0:-1::0:-1:1:0:6:android.media.mediacodec.codec=OMX.google.aac.encoder:android.media.mediacodec.mode=audio:android.media.mediacodec.secure=0:android.media.mediacodec.encoder=1:android.media.mediacodec.mime=audio/mp4a-latm:android.media.mediacodec.latency.unknown=8:] [forcenew=0] I/ACodec (25781): ~ACodec() I/MPEG4Writer(25781): reset() D/MPEG4Writer(25781): Audio track stopping. Stop source D/MPEG4Writer(25781): Audio track source stopping D/MPEG4Writer(25781): Audio track source stopped D/MPEG4Writer(25781): Audio track stopped. Stop source D/MPEG4Writer(25781): Stopping writer thread D/MPEG4Writer(25781): 0 chunks are written in the last batch D/MPEG4Writer(25781): Writer thread stopped I/MPEG4Writer(25781): release() D/AndroidRuntime(25781): Shutting down VM E/AndroidRuntime(25781): FATAL EXCEPTION: main E/AndroidRuntime(25781): Process: com.llfbandit.record_example, PID: 25781 E/AndroidRuntime(25781): java.lang.IllegalStateException: Failed to stop the muxer E/AndroidRuntime(25781): at android.media.MediaMuxer.nativeStop(Native Method) E/AndroidRuntime(25781): at android.media.MediaMuxer.stop(MediaMuxer.java:454) E/AndroidRuntime(25781): at com.llfbandit.record.record.container.MuxerContainer.stop(MuxerContainer.kt:19) E/AndroidRuntime(25781): at com.llfbandit.record.record.encoder.MediaCodecEncoder.internalStop(MediaCodecEncoder.kt:86) E/AndroidRuntime(25781): at com.llfbandit.record.record.encoder.MediaCodecEncoder.onOutputBufferAvailable(MediaCodecEncoder.kt:143) E/AndroidRuntime(25781): at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1703) E/AndroidRuntime(25781): at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1650) E/AndroidRuntime(25781): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(25781): at android.os.Looper.loop(Looper.java:227) E/AndroidRuntime(25781): at android.app.ActivityThread.main(ActivityThread.java:7275) E/AndroidRuntime(25781): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(25781): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497) E/AndroidRuntime(25781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908) E/flutter (25781): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(-1, writeSampleData returned an error, java.lang.IllegalStateException: writeSampleData returned an error E/flutter (25781): at android.media.MediaMuxer.nativeWriteSampleData(Native Method) E/flutter (25781): at android.media.MediaMuxer.writeSampleData(MediaMuxer.java:682) E/flutter (25781): at com.llfbandit.record.record.container.MuxerContainer.writeSampleData(MuxerContainer.kt:29) E/flutter (25781): at com.llfbandit.record.record.encoder.MediaCodecEncoder.onOutputBufferAvailable(MediaCodecEncoder.kt:132) E/flutter (25781): at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1703) E/flutter (25781): at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1650) E/flutter (25781): at android.os.Handler.dispatchMessage(Handler.java:106) E/flutter (25781): at android.os.Looper.loop(Looper.java:227) E/flutter (25781): at android.app.ActivityThread.main(ActivityThread.java:7275) E/flutter (25781): at java.lang.reflect.Method.invoke(Native Method) E/flutter (25781): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497) E/flutter (25781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908) E/flutter (25781): , null) E/flutter (25781): I/Process (25781): Sending signal. PID: 25781 SIG: 9 Lost connection to device. the Dart compiler exited unexpectedly.

For your information, I did not experience this crash on other devices I have (with Android 12 and Android 14). Thank you!

mniiinm commented 5 months ago

same error in android 8 and 9. java.lang.IllegalStateException: Failed to stop the muxer Stack traces: android.media.MediaMuxer.nativeStop MediaMuxer.java android.media.MediaMuxer.stop MediaMuxer.java:454 m6.d.b o6.c.g o6.c.onOutputBufferAvailable android.media.MediaCodec$EventHandler.handleCallback MediaCodec.java:1699 android.media.MediaCodec$EventHandler.handleMessage MediaCodec.java:1646 android.os.Handler.dispatchMessage Handler.java:106 android.os.Looper.loop Looper.java:201 android.app.ActivityThread.main ActivityThread.java:6831 java.lang.reflect.Method.invoke Method.java com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:547 com.android.internal.os.ZygoteInit.main ZygoteInit.java:927