Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkOnMainThread Exception #18

Closed
sidzi opened this issue Sep 28, 2016 · 20 comments
Closed

NetworkOnMainThread Exception #18

sidzi opened this issue Sep 28, 2016 · 20 comments
Labels

Comments

@sidzi
Copy link

sidzi commented Sep 28, 2016

if (mjpegView.isStreaming()) {
                mjpegView.stopPlayback();
            }

causes the exception and cannot be called in async task or any other worker thread i.e. has to be called from the UI thread :( , How to fix this ?

@niqdev
Copy link
Owner

niqdev commented Sep 28, 2016

Hi! Can you please give me more details about the stacktrace and tell me the exact steps for reproduce the error? Thanks

@sidzi
Copy link
Author

sidzi commented Sep 29, 2016

yeah sure,
I have to close/stop mjpegView after I press the back button in the activity i.e. in the onBackPressed() method and so I have to run this code

@Override
    public void onBackPressed() {
            if (mjpegView.isStreaming()) {
                mjpegView.stopPlayback();
            }
        super.onBackPressed();
    }

but this gives me an exception , since I cannot do network ops on the main UI thread (which the isStreaming() function is probably doing) , so what should I do ?

@niqdev
Copy link
Owner

niqdev commented Sep 29, 2016

I understood now, the real problem is in the implementation that this library wrap and the Thread management.
I would suggest to fork the project, add your code on a separate branch and try to debug MjpegViewDefault. In this way I will be able to help you and fix the issue.
Actually isStreaming() is at line 330 and doesn't seem to be the problem, maybe stopPlayback().
Thanks

@sidzi
Copy link
Author

sidzi commented Oct 19, 2016

really sorry for the delay.
I tried running the forked project and found that the exception was occurring at line 221 in MjpegViewDefault.

if (mIn != null) {
            try {
                mIn.close(); /* This line */
            } catch (IOException e) {
            }
            mIn = null;
        }

Should the mIn.close be in a different worker thread ?

@niqdev
Copy link
Owner

niqdev commented Oct 19, 2016

no problem, can you please push the branch with the implementation on your remote?

Moreover, can you please try to downgrade the library to previous versions one by one and see if you have the same problem? I checked the commit and I wouldn't that we have introduced some regressions

Thanks

@niqdev
Copy link
Owner

niqdev commented Oct 24, 2016

Hi @sidzi , do you still have the problem? Did you try to downgrade the library one by one?
Thanks

@niqdev
Copy link
Owner

niqdev commented Oct 24, 2016

@sidzi I'm investigating your error, I have it only on old android versions like 18, not on the latest, that's why I wasn't able to reproduce it.
I'm trying to debugging this

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mjpegView.isStreaming()) {
                    mjpegView.stopPlayback();
                }
                break;
        }
        return true;
    }

Can you just confirm please your sdk version with the error?
Thanks

@niqdev
Copy link
Owner

niqdev commented Oct 25, 2016

@sidzi
Copy link
Author

sidzi commented Oct 26, 2016

I wasn't able to solve the issue , I am running android 6.0.1 (api 23)
I will look through the links you have posted and tell you if I find anything .

also I believe the issue can be solved by putting the code you are debugging outside the main thread but it is resulting in another error , and I am not good with java , (I know how I can solve it , but I need some java advice).

@danieltigse
Copy link

danieltigse commented Nov 12, 2016

I have the same issue on my samsung galaxy s6 running android 6.0.1
BTW: Not happening in version 0.3.4

If I user version 0.3.4 I am not able to resume the playback.
When I start the video again the threadvariable is null:

void _startPlayback() {
        if (mIn != null && thread != null && thread.getState() == Thread.State.NEW) {
            mRun = true; //NEVER ENTERS HERE
            thread.start();
        }
    }

@niqdev
Copy link
Owner

niqdev commented Nov 13, 2016

@sidzi and @danieltigse couple of weeks ago I tried to update the UI on this unstable branch but I gave up, I wasted to much time trying to debug and fix this issue.
At this point the only thing to do probably is to refactor or better rewrite from scratch

because all the issues are mainly related to the underlying implementation.
If you fill brave enough you are free to fork the repo and make the changes on a new branch, otherwise I will try to do it soon or later

@sidzi
Copy link
Author

sidzi commented Nov 14, 2016

@niqdev I don't know if I have enough knowledge to do this myself , but if you'd like to , I can pair program with you whenever you decide to work on it , if that helps :)

@danieltigse
Copy link

My only workaround is use the 0.3.4 version, the only version where the stopPlayback method works. And the trick is that when I want to play again I remove and create the view again.

@niqdev
Copy link
Owner

niqdev commented Nov 14, 2016

Thanks @danieltigse for point out the version.
By the way all changes from 0.3.x up to the latest are all related to the same problem.
I would definitely like to rewrite it anyway in future!

@niqdev
Copy link
Owner

niqdev commented Jan 14, 2017

Hi @danieltigse and @sidzi , I just published version 0.4.0 to solve and issue to manage multiple view in the same activity. Could you please tell me if with the new version you still have the same problems?
Thanks

@PhanPirang
Copy link

Hi @niqdev, now I can use stopPlayback() method. Thanks for your hard working! Anyway I have another problem when I call freeCameraMemory in OnDestroy method, the app crushed!

@niqdev
Copy link
Owner

niqdev commented Jan 18, 2017

@PhanPirang yes, is not supported, here is the implementation

@Override
public void freeCameraMemory() {
  throw new UnsupportedOperationException("not implemented");
}

Actually this method is there only because the initial plan was to wrap also the native library

@niqdev
Copy link
Owner

niqdev commented Jan 18, 2017

Since it seems to work now I will close the issue and update also the app soon.
Feel free to re-open the issue or create a new one if you have other problems

@niqdev niqdev closed this as completed Jan 18, 2017
@niqdev
Copy link
Owner

niqdev commented Jan 18, 2017

@sidzi @danieltigse @PhanPirang If you are working on an open source project, would you be interested to add in the Readme the link to your app?

@sidzi
Copy link
Author

sidzi commented Jan 18, 2017

No , I wasn't , and thank you for the effort 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants