最近在公司实习做的视频播放,记录一下。直接上代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
>
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="@+id/video_view"
/>
</FrameLayout>
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private VideoView mVideoView;
private int mLastPlayedTime;
private final String LAST_PLAYED_TIME = "LAST_TIME";
//static int pos = 1;
private int count = 0;
private List<String> listFileName = new ArrayList<String>();
private String path = Environment.getExternalStorageDirectory().getPath() + "/video/";
Random r = new Random();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
mVideoView = (VideoView) findViewById(R.id.video_view);
MediaController controller = new MediaController(this);
mVideoView.setMediaController(controller);
controller.setMediaPlayer(mVideoView);
getAllFilename(path, listFileName);
//pos=listFileName.get();
if (listFileName.get(count) != null) {
playFristVideo();
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override//播放完毕之后触发的方法
public void onCompletion(MediaPlayer mediaPlayer) {
playVideo();
if(count==(listFileName.size()-1)){
Log.d("MAO", "重新进入第一个视频播放");
playFristVideo();
}
else {
playVideo();
}
}
});
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
exit();
return true;
}
});
}
}
private void playFristVideo() {
mVideoView.setVideoURI(Uri.parse(path+listFileName.get(0)));
mVideoView.start();
mVideoView.requestFocus();
}
public void playVideo() {
Toast.makeText(this, listFileName.get(count), Toast.LENGTH_LONG).show();
Log.d("MAO", listFileName.get(count));
count = r.nextInt(listFileName.size());
Uri uri = Uri.parse(path + listFileName.get(count));
mVideoView.setVideoURI(uri);
mVideoView.start();
mVideoView.requestFocus();
}
public void getAllFilename(String path, List<String> fileName) {
File file = new File(path);
File[] files = file.listFiles();
String[] names = file.list();
if (names != null)
fileName.addAll(Arrays.asList(names));
for (File a : files) {
if (a.isDirectory()) {
getAllFilename(a.getName().toString(), fileName);
}
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
default:
return super.onOptionsItemSelected(item);
}
}
private void exit() {
Toast.makeText(this, "播放视频错误", Toast.LENGTH_SHORT).show();
finish();
}
protected void onPause() {
super.onPause();
mVideoView.pause();
mLastPlayedTime = mVideoView.getCurrentPosition();
}
protected void onResume() {
super.onResume();
mVideoView.start();
if (mLastPlayedTime > 0) {
mVideoView.seekTo(mLastPlayedTime);
}
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(LAST_PLAYED_TIME, mVideoView.getCurrentPosition());
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mLastPlayedTime = savedInstanceState.getInt(LAST_PLAYED_TIME);
}
}