Fatal error on back button/pause android.os.NetworkOnMainThreadException
yeahman45 opened this issue ยท 2 comments
I am using the same code as the sample app :
https://github.com/niqdev/ipcam-view/blob/master/app/src/main/java/com/github/niqdev/ipcam/IpCamDefaultActivity.java
i.e. stopPlayback onPause
But when it pauses/back button, I get the following fatal exception:
2019-12-15 22:17:14.573 29392-29392/com.myapp.app E/MessageQueue-JNI: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:325) at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:724) at com.android.okhttp.okio.Okio$2.read(Okio.java:142) at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:218) at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:56) at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.read(Http1xStream.java:886) at com.android.okhttp.internal.Util.skipAll(Util.java:159) at com.android.okhttp.internal.Util.discard(Util.java:141) at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.close(Http1xStream.java:919) at com.android.okhttp.okio.RealBufferedSource.close(RealBufferedSource.java:402) at com.android.okhttp.okio.RealBufferedSource$1.close(RealBufferedSource.java:390) at java.io.BufferedInputStream.close(BufferedInputStream.java:485) at java.io.FilterInputStream.close(FilterInputStream.java:181) at com.github.niqdev.mjpeg.MjpegViewDefault._stopPlayback(MjpegViewDefault.java:276) at com.github.niqdev.mjpeg.MjpegViewDefault.stopPlayback(MjpegViewDefault.java:417) at com.github.niqdev.mjpeg.MjpegSurfaceView.stopPlayback(MjpegSurfaceView.java:134) at com.myapp.app.fragments.CameraViewFragment.onPause(CameraViewFragment.java:108)
I don't know if this causes the following problem:
When I view a mjpeg stream and I go back. I re-view the mjpeg stream, the fps drops. The more I go back and re-view the mjpeg stream, the more the fps drops until it becomes unwatchable.
I think I found the solution.
I run the mjpegView.stopPlayback() in a thread. There is no more the fatal exception and there's no more the FPS drop issue.
Following your idea, I apply the code as follow and the issue is gone:
override fun onPause() {
super.onPause()
GlobalScope.launch(Dispatchers.IO) {
binding.mjpegView.stopPlayback()
}
}