package com.example.myvideo2;

 import java.io.File;

 import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.VideoView;
/**
* Android 播放视频
* @Describe:
* @package: com.example.myvideo2
* @author shaobn
* @date 2015-9-14 上午10:57:41
*/
public class MainActivity extends Activity implements MediaPlayerControl {
private VideoView videoView;
private MediaController mediaController;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaController = new MediaController(MainActivity.this);
videoView = (VideoView) this.findViewById(R.id.videoView1);
button = (Button) this.findViewById(R.id.button1);
videoView.setMediaController(mediaController);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file1 = Environment.getExternalStorageDirectory();
File file2 = new File(file1,"/Movies/Video.mp4");
Uri uri = Uri.fromFile(file2);
videoView.setVideoURI(uri);
}
}
}); }
@Override
public boolean canPause() {
// TODO Auto-generated method stub
return videoView.canPause();
}
@Override
public boolean canSeekBackward() {
// TODO Auto-generated method stub
return videoView.canSeekBackward();
}
@Override
public boolean canSeekForward() {
// TODO Auto-generated method stub
return videoView.canSeekForward();
}
@Override
public int getAudioSessionId() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getBufferPercentage() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getCurrentPosition() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getDuration() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isPlaying() {
// TODO Auto-generated method stub
return videoView.isPlaying();
}
@Override
public void pause() {
// TODO Auto-generated method stub
if (videoView.isPlaying()) {
videoView.pause();
}
}
@Override
public void seekTo(int arg0) {
// TODO Auto-generated method stub
videoView.seekTo(arg0);
}
@Override
public void start() {
// TODO Auto-generated method stub
videoView.start();
} }

实现播放视频文件。

05-11 11:35