只是上上手而已的例子。
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
<!--为屏蔽home健而加-->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
package com.chenchen.app;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
private Context context;
private VideoView videoView;
/*视频文件分别放在三个文件夹中,需要到时候全部放入一个list中随机播放*/
private static final String MUSIC_PATH_1 = new String("/sdcard/qingbao/1/");
private static final String MUSIC_PATH_2 = new String("/sdcard/qingbao/2/");
private static final String MUSIC_PATH_3 = new String("/sdcard/qingbao/3/");
private List<String> mMusicList = new ArrayList<String>();
private int curIndex = 0;//
Random r=new Random();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listFiles();
}
/* 播放列表 */
private void listFiles() {
// 取得指定位置的文件设置显示到播放列表
File path_1 = new File(MUSIC_PATH_1);
File[] list = path_1.listFiles(new MusicFilter());
if (list.length > 0) {
for (File file : list) {
mMusicList.add(MUSIC_PATH_1+file.getName());
}
}
File path_2 = new File(MUSIC_PATH_2);
list = path_2.listFiles(new MusicFilter());
if (list.length > 0) {
for (File file : list) {
mMusicList.add(MUSIC_PATH_2+file.getName());
}
}
File path_3 = new File(MUSIC_PATH_3);
list = path_3.listFiles(new MusicFilter());
if (list.length > 0) {
for (File file : list) {
mMusicList.add(MUSIC_PATH_3+file.getName());
}
}
}
/* 过滤文件类型 */
class MusicFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
// 这里还可以设置其他格式的文件
return (name.endsWith(".mp4"));
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(videoView!=null&&videoView.isPlaying()){
videoView.pause();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
startVideo();
}
private void startVideo(){
videoView = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(context);
videoView.setMediaController(mc);
// videoView.setVideoURI(Uri.parse("http://....h.3gp"));
videoView.requestFocus();
play();
videoView
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
play();//播放完毕再继续下一首
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
play();//播放出错再继续下一首
return true;
}
});
}
private void play(){
curIndex=r.nextInt(mMusicList.size());
videoView.setVideoPath(mMusicList.get(curIndex));
videoView.start();
}
private long waitTime = 2000;
private long touchTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& KeyEvent.KEYCODE_BACK == keyCode) {
long currentTime = System.currentTimeMillis();
if ((currentTime - touchTime) >= waitTime) {
Toast.makeText(context, "再按一次退出程序", Toast.LENGTH_SHORT).show();
touchTime = currentTime;
} else {
finish();
System.exit(0);
}
return true;
}else if(KeyEvent.KEYCODE_HOME == keyCode){
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onAttachedToWindow () {
/*本想屏蔽Home键,加上后播放器没画面,但有声音,why?*/
// this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <VideoView android:id="@+id/videoView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
<!--为屏蔽home健而加-->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>