使用videoview播放資源文件夾下的視頻


首先,視屏放在res下的raw目錄中

//資源文件夾下res===raw視屏播放
public class MainActivity extends Activity {
private static int currentVideo=0;
private VideoView videoview;
private ArrayList<Uri> mList;
private MediaController mc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
videoview=(VideoView)findViewById(R.id.video);
mc = new MediaController(MainActivity.this);
//請求res下raw目錄下的多個視屏循環,自動依次播放
mList = new ArrayList<Uri>();
mList.add(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.b));
mList.add(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.c));
mList.add(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.d));
mList.add(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.e));
videoview.setVideoURI(mList.get(currentVideo));
videoview.setMediaController(mc);
videoview.requestFocus();
try {
videoview.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
videoview.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
nextVideo();
}
private void nextVideo() {
// TODO Auto-generated method stub
currentVideo++;
if (currentVideo==mList.size()) {
currentVideo=0;
}
videoview.setVideoURI(mList.get(currentVideo));
videoview.setMediaController(mc);
videoview.requestFocus();
videoview.start();
}
});



//自動播放res下的raw目錄下的單個視頻,循環播放
// videoview.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.john));
// videoview.setMediaController(mc);
// videoview.requestFocus();
// videoview.start();
// videoview.setOnCompletionListener(new OnCompletionListener() {
//
// @Override
// public void onCompletion(MediaPlayer mp) {
// // TODO Auto-generated method stub
// videoview.start();
// }
// });
}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM