Issue
I am trying to develop a Video Streaming Application using Firebase which stores video into Firebase storage and retrieves it from there by parsing it into an URI.
Problem
I am trying to execute an AsyncTask inside onPostExecute of another AsyncTask and receiving this following error:
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.getCurrentPosition(Native Method)
at android.widget.VideoView.getCurrentPosition(VideoView.java:881)
at com.foodies.mohitgupta.foodyyoucantstsyhungry.KitchenVideoView$ViewProgresss$override.doInBackground(KitchenVideoView.java:186)
at com.foodies.mohitgupta.foodyyoucantstsyhungry.KitchenVideoView$ViewProgresss$override.access$dispatch(KitchenVideoView.java)
at com.foodies.mohitgupta.foodyyoucantstsyhungry.KitchenVideoView$ViewProgresss.doInBackground(KitchenVideoView.java:0)
at com.foodies.mohitgupta.foodyyoucantstsyhungry.KitchenVideoView$ViewProgresss.doInBackground(KitchenVideoView.java:176)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
Here is the first AsyncTask implementation on which onPostExecute() method I tried to execute the second AsyncTask:
class playy extends AsyncTask<String,Void,Uri>
{
@Override
protected Uri doInBackground(String... strings) {
Uri u=Uri.parse(strings[0]);
return u;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onPostExecute(Uri uri) {
super.onPostExecute(uri);
vvv=new ViewProgresss();
video.setVideoURI(uri);
video.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == mp.MEDIA_INFO_VIDEO_RENDERING_START) {
pmain.setVisibility(View.GONE);
return true;
} else if (what == mp.MEDIA_INFO_BUFFERING_START) {
pmain.setVisibility(View.VISIBLE);
return true;
} else if (what == mp.MEDIA_INFO_BUFFERING_END) {
pmain.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
video.requestFocus();
video.start();
isPlaying = true;
play.setImageResource(R.drawable.ic_media_play);
new ViewProgresss().execute();
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
duration = mp.getDuration() / 1000;
String durationString = String.format("%02d:%02d", duration / 60, duration % 60);
TotalVideo.setText(durationString);
}
});
}
}
Here is the second AsyncTask details with the onBackPressed() method:
public class ViewProgresss extends AsyncTask<Void,Integer,Void>
{
@Override
protected Void doInBackground( Void... voids ) {
do{
if(isCancelled()){
break;}
current = video.getCurrentPosition() / 1000;
try {
publishProgress(current);
} catch (Exception e) {
}
}while(pvideo.getProgress()<=100);
return null;
}
@Override
protected void onProgressUpdate( Integer... values ) {
super.onProgressUpdate(values);
try{
int currentPro=current* 100/duration;
pvideo.setProgress(currentPro);
String CurrentTime=String.format("%02d:%02d",values[0]/60,values[0]%60);
StartVideo.setText(CurrentTime);
}catch (Exception e)
{
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
vvv.cancel(true);
video.stopPlayback();
isPlaying=false;
}
Solution
"video.getCurrentPosition()" cannot be executed until the "onPrepared()" method is called, and that's why you get this Exception: the file is not loaded in the MediaPlayer yet.
Answered By - emandt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.