<pre name="code" class="java">package com.stephen.android_35_playvideoview;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final File FOLDER = getFolder();
private List<String> lists_Path = null;
MediaController mediaco;
private int index = 0;
private VideoView myVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView) this.findViewById(R.id.myVideoview_main);
Log.i(TAG, "-------->>" + "文件路径为: " + FOLDER);
lists_Path = getFilePath(FOLDER);
myVideoView.requestFocus();
mediaco = new MediaController(this);
mediaco.setMediaPlayer(myVideoView);
myVideoView.setMediaController(mediaco);
initVideoView();
/**
* <span style="font-size:14px;color:#3333ff;">此监听器监听path路径设置好后调用的方法,一般做 "开始播放的操作"</span>
*/
myVideoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.i(TAG, "------->>onPrepared此方法被调用了!·");
myVideoView.start();
}
});
/**
* <span style="font-size:14px;color:#3333ff;">此监听器监听视频无法播放时调用的方法,一般做 "错误处理的操作"</span>
*/
myVideoView.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.i(TAG, "-------->>" + "onError: " + index);
playVideo();
return true;
}
});
/**
* <span style="font-size:14px;color:#3333ff;">此监听器监听视频播放完成调用的方法,一般做 "重新设置uri路径或者path路径</span>
*/
myVideoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.i(TAG, "-------->>" + "index为: " + index);
playVideo();
}
});
}
/**
* 初始化VideoView
*/
private void initVideoView() {
// TODO Auto-generated method stub
myVideoView.setVideoPath(lists_Path.get(index++ % lists_Path.size()));
}
/**
* 获取视频文件夹
*
* @return
*/
private static File getFolder() {
File rooFile = Environment.getExternalStorageDirectory();
if (rooFile != null) {
File new_file = new File(rooFile, "letv" + File.separator
+ "storage" + File.separator + "download" + File.separator
+ "info");
return new_file;
}
return null;
}
/**
* 得到文件夹里的所有视频路径(这里只支持mp4和3gp两种格式)
*
* @param folder
* @return
*/
private List<String> getFilePath(File folder) {
List<String> list_FilePaths = null;
if (folder.exists()) {
list_FilePaths = new ArrayList<String>();
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
String temp_Path = files[i].getAbsolutePath();
if (temp_Path.endsWith(".mp4") || temp_Path.endsWith(".3gp")) {
list_FilePaths.add(temp_Path);
}
}
Log.i(TAG, "-------->>" + "所有文件路径为: " + list_FilePaths.size()
+ "\n" + list_FilePaths);
return list_FilePaths;
}
return null;
}
/**
* 开始播放视频
*/
private void playVideo() {
myVideoView.setVideoPath(lists_Path.get(index++ % lists_Path.size()));
}
}
Android_关于VideoView
最新推荐文章于 2021-05-28 17:43:52 发布