android - videoView只执行一次AsyncTask / onPostExecute永远不会被调用

时间:2014-05-12 16:23:35

标签: android android-asynctask android-videoview

我有3个视频。取决于videoLong我使用videoView播放所选视频。

我有两个问题:

  1. 第一次播放视频时,它有效。视频会在正确的时间停止,并使用finish()。但是在doInBackground之后的任何时间都没有检查其他if语句。

  2. 我的onPostExecute永远不会被调用。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (videoLong == 0) {
        videoView
                .setVideoURI(Uri
                        .parse("http://pv...70ab1aae7633.mp4"));
        videoView.seekTo(60000 * 10);// 10 mins
        videoView.start();
        new myAsync().execute();
    } else if (videoLong == 1) {
    
        videoView
                .setVideoURI(Uri
                        .parse("http://pv....mp4"));
        videoView.seekTo(60000 * 10);// 10 mins
        videoView.start();
        new myAsync().execute();
    } else if (videoLong == 2) {
        videoView
                .setVideoURI(Uri
                        .parse("http://pv....mp4"));
        videoView.seekTo(60000 * 10);// 10 mins
    
        new myAsync().execute();
        videoView.start();
    }
    }
    
    private class myAsync extends AsyncTask<Void, Integer, Void> {
    
    int duration = 0;
    int current = 0;
    
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("on pre execute");
        // alertDialog = new AlertDialog.Builder(PlayVideoActivity.this);
    }
    
    @Override
    protected Void doInBackground(Void... params) {
    
        videoView
                .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    
                    public void onPrepared(MediaPlayer mp) {
                        duration = videoView.getDuration();
    
                    }
                });
    
        videoView
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        // Do whatever u need to do here
                        System.out.println("video complete");
                        finish();
                    }
                });
    
        do {
            current = videoView.getCurrentPosition();
            // System.out.println("duration - " + duration + " current- " +
            // current);
    
            try {
    
                publishProgress((int) (current * 100 / duration));
    
                // System.out.println("time: " + current);
                if (current >= 610100 && videoLong == 0) {
                    System.out.println("video 1 done");
                    videoView.stopPlayback();
    
                    videoView = null;
                }
                if (current >= 610200 && videoLong == 1) {
                    System.out.println("video 2 done");
                    videoView.stopPlayback();
                    finish();
                }
                if (current >= 610200 && videoLong == 2) {
                    System.out.println("video 3 done");
                    videoView.stopPlayback();
                    finish();
                }
                if (mProgressBar.getProgress() >= 100) {
                    // System.out.println("error100");
                }
    
            } catch (Exception e) {
                // e.printStackTrace();
            }
        } while (mProgressBar.getProgress() <= 100);
    
        return null;
    }
    
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        // System.out.println("values: " + values[0]);
        mProgressBar.setProgress(values[0]);
    }
    
    @Override
    protected void onPostExecute(Void result) {
        System.out.println("on POST execute");
    
    
    }
    
    }
    
    @Override
     public void onBackPressed() {
    if (videoView != null)
        videoView.stopPlayback();
    videoView = null;
    System.out.println("back button pressed. stop playback");
    super.onBackPressed();
    }
    
  3. 感谢您的期待!

0 个答案:

没有答案