My app keeps telling me that I'm doing too much work on the main thread, and if I don't wait long enough when the app first loads before touching the UI, it crashes. Specifically, it seems like what I'm waiting for is the video to load into the videoView, because when I comment out the 'myVideoView.setVideoURI' line, it works totally fine.
I already have an asynctask set up for communicating with the server, so I thought maybe I could some how use an asynctask to set the video in the background and then hide the progress bar when it's done.
I tried putting my entire video loading code below inside the asynctask, but it kept saying 'this must be done on the UI thread'. Of course, I'm probably just misunderstanding the concept of multi threading, but if anyone could help me clarify how I may go about reducing the strain on my main thread, that would be so great. Thank you!
Here is my code sample. All of this is currently inside onCreate.
myVideoView = (VideoView) findViewById(R.id.videoView);
try {
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
myVideoView.seekTo(0);
}
});
// EMPTY ASYNCTASK
public class LoadVideo extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// DO SOMETHING BEFORE IT STARTS
}
protected String doInBackground(String... args) {
// DO HEAVY LIFTING
return null;
}
protected void onPostExecute(String file_url) {
// DO SOMETHING AFTER IT FINISHES
}
}