2015-10-17 72 views
0

在onCreate()方法中,我確實啓動了一個線程來播放視頻,並且希望線程知道視頻播放已完成。Android VideoView,如何理解播放完成

這是如何實現的?

這裏是我的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_mode1); 

    videoView = (VideoView) findViewById(R.id.videoView); 

    Thread t = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      continueLoop = true; 
      while (continueLoop) { 
       String[] allVideoFiles = {"video1.mp4", "video2.mp4", "video3.mp4", "video4.mp4", "video5.mp4"}; 
       for (String video : allVideoFiles) { 
        final String v = video; 
        File f = new File("/blablabla" + video); 
        int duration = 0; 
        if (!f.exists()) { 
         continue; 
        } 
        duration = MediaPlayer.create(getApplicationContext(), Uri.fromFile(f)).getDuration(); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          videoView101.setVisibility(View.VISIBLE); 

          MediaController ctrl = new MediaController(getApplicationContext()); 
          ctrl.setVisibility(View.GONE); 
          videoView.setMediaController(ctrl); 
          videoView.setVideoPath("/blablabla" + v); 
          videoView.requestFocus(); 

          videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
           @Override 
           public void onPrepared(MediaPlayer mp) { 
            mp.setLooping(false); 
            videoView101.start(); 
           } 
          }); 

          videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
           @Override 
           public void onCompletion(MediaPlayer mp) { 
            // how to tell the thread that the video is over??? 
           } 
          }); 

          videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { 
           @Override 
           public boolean onError(MediaPlayer mp, int what, int extra) { 
            Log.v(TAG, "ERROR: " + what + " " + extra); 
            return false; 
           } 
          }); 
         } 
        }); 
        // wait for video play 
        try { 
         Thread.sleep(duration); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 
    }); 
    t.start(); 
} 

注:我不能設置 「continueLoop」 爲false onCompletion(MediaPlayer的MP)...

我希望有我的線程控制那個行動。例如,該線程將發送電子郵件,或顯示吐司等

感謝,

+0

你不需要線程,使用的什麼特別的原因? – pskink

+0

同意。沒有特別的理由!我看到你的觀點,我認爲這個工作(連續播放多個視頻)可以通過在主類上實現「OnCompletionListener」來完成,對吧? – FBG

回答

0

剛剛發現的answer,看起來效果很好...

公共類MyVideoActivity延伸活動{

public final static String DIR = "/blablabla/"; 

final String[] allVideoFiles = {"video001.mp4", "video002.mp4", "video003.mp4"}; 

private long c = -1; 
VideoView videoView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my_video); 

    videoView = (VideoView) findViewById(R.id.videoView001); 

    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
      play_video(); 
     } 
    }); 

    play_video(); 
} 

private void play_video() { 
    c++; 
    int index = (int) (c % allVideoFiles.length); 
    String file = DIR + allVideoFiles[index]; 
    File f = new File(file); 
    if (!f.exists()) { 
     Log.v("VIDEO", "File does NOT exist " + file); 
     return; 
    } 

    videoView.setVideoPath(file); 
    videoView.requestFocus(); 
    videoView.setVisibility(View.VISIBLE); 
    videoView.start(); 

} 

}