从另一个AsyncTask的onPostExecute方法执行AsyncTask时出错

时间:2018-12-18 13:05:41

标签: android android-asynctask

我正在尝试使用Firebase开发视频流应用程序,该应用程序将视频存储到Firebase存储中,并通过将其解析为URI从那里检索视频。

问题

我试图在另一个AsyncTask的onPostExecute内执行一个AsyncTask并收到以下错误:

@home.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_post(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    form.tag.choices = [(tag.id, tag.name) for tag in Tag.query.order_by('name')]
    if form.validate_on_submit():
        post.body = form.body.data
        posttags = db.session.query(categories).filter_by(post_id=id).all()
        for id in form.tag.data:
            if (post.id, id) not in posttags:
                post.tags.append(Tag.query.get(id))
        db.session.commit()
        return redirect(url_for('.view_post', id=post.id))
    form.body.data = post.body
    # already selected values
    form.tag.data = [tag_id for tag_id in db.session.query(categories).filter_by(post_id=post.id).all()]
    return render_template('home/edit_post.html', form=form, title='Edit Post')

这是我尝试在其上执行第二个AsyncTask的 onPostExecute()方法的第一个AsyncTask实现:

 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)

这是使用 onBackPressed()方法的第二个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);
                }
            });

        }
    }

2 个答案:

答案 0 :(得分:1)

在调用“ onPrepared()”方法之前,无法执行“ video.getCurrentPosition()”,这就是为什么您会收到此异常的原因:该文件尚未加载到MediaPlayer中。 < / p>

答案 1 :(得分:0)

正如emandt所述,您不应在第二个AsyncTask中调用 video.getCurrentPosition()方法,直到您在第一个AsyncTask中收到 onPrepared()方法的回调AsyncTask。为了更精确,在 playy AsyncTask中的 onPrepared()方法内调用 ViewProgresss AsyncTask执行。

这是您的第一个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);

            video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    new ViewProgresss().execute();
                    duration = mp.getDuration() / 1000;
                    String durationString = String.format("%02d:%02d", duration / 60, duration % 60);
                    TotalVideo.setText(durationString);
                }
            });
        }
    }