CasperPas / flutter-sound-stream

stream audio data in & out
GNU General Public License v3.0
84 stars 111 forks source link

Parameter specified as non-null is null #26

Closed tmxdyf closed 7 months ago

tmxdyf commented 3 years ago

Crash when exiting the app.

 Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter recorder
W/System.err(23631):    at vn.casperpas.sound_stream.SoundStreamPlugin$createRecordListener$1.onMarkerReached(Unknown Source:2)
W/System.err(23631):    at vn.casperpas.sound_stream.SoundStreamPlugin.onDetachedFromEngine(SoundStreamPlugin.kt:131)
W/System.err(23631):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:262)
W/System.err(23631):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:270)
W/System.err(23631):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.removeAll(FlutterEngineConnectionRegistry.java:278)
W/System.err(23631):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.destroy(FlutterEngineConnectionRegistry.java:122)
W/System.err(23631):    at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:375)
W/System.err(23631):    at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach(FlutterActivityAndFragmentDelegate.java:572)
W/System.err(23631):    at io.flutter.embedding.android.FlutterActivity.release(FlutterActivity.java:587)
W/System.err(23631):    at io.flutter.embedding.android.FlutterActivity.onDestroy(FlutterActivity.java:608)
W/System.err(23631):    at android.app.Activity.performDestroy(Activity.java:8225)
W/System.err(23631):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1342)
W/System.err(23631):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5441)

image

sadfarhan124 commented 3 years ago

Any luck with solving this?

freedom6xiaobai commented 1 year ago

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { try { methodChannel.setMethodCallHandler(null) mListener?.onPeriodicNotification(null) mListener = null mRecorder?.let { it.stop() it.release() } mRecorder = null }catch (e: Exception){ debugLog("onDetachedFromEngine error: ${e.localizedMessage}") } }

上面的代码是一个 Flutter 插件的函数 onDetachedFromEngine 的实现。它在 Flutter 引擎从宿主分离(detach)时被调用。下面是对代码的解析:

  1. methodChannel.setMethodCallHandler(null):这行代码将 methodChannel 对象的方法调用处理程序设置为 null,即取消对方法调用的处理。

  2. mListener?.onPeriodicNotification(null):这行代码通过调用 mListener 对象的 onPeriodicNotification 方法,向侦听器发送一个空参数的定期通知。

  3. mListener = null:将 mListener 对象设置为 null,释放对侦听器的引用。

  4. mRecorder?.let { ... }:这是一个安全调用,如果 mRecorder 不为 null,则执行花括号中的代码。

  5. it.stop():调用 mRecorder 对象的 stop 方法,停止录制音频。

  6. it.release():调用 mRecorder 对象的 release 方法,释放录制器的资源。

  7. mRecorder = null:将 mRecorder 对象设置为 null,释放对录制器的引用。

  8. catch (e: Exception) { ... }:如果在上述过程中发生异常,将捕获异常并执行花括号中的代码。在这里,它会调用一个名为 debugLog 的方法,将异常的本地化消息打印到调试日志中。

这段代码的目的是在 Flutter 插件与宿主分离时执行一些清理操作,停止录音并释放资源。

CasperPas commented 7 months ago

@freedom6xiaobai Thanks for the fix. I modified the code. Will deploy on the next version.