首页 > 代码库 > Android播放本地视频

Android播放本地视频

代码如下:

public class MainActivity extends Activity implements MediaPlayerControl {

?

????private VideoView videoView;

????private MediaController mediaController;

?

????@Override

????protected void onCreate(Bundle savedInstanceState) {

????????// TODO Auto-generated method stub

????????super.onCreate(savedInstanceState);

????????setContentView(R.layout.mainactivity);

????????videoView = (VideoView) findViewById(R.id.videoView1);

????????mediaController = new MediaController(this);

????????videoView.setMediaController(mediaController);

????????// 判断是否挂载sdcard

????????if (Environment.getExternalStorageState().equals(

????????????????Environment.MEDIA_MOUNTED)) {

????????????File file = Environment.getExternalStorageDirectory();// sdcard根目录

????????????File videoFile = new File(file, "test.mp4");

????????????Uri uri = Uri.fromFile(videoFile);// 获取uri

????????????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 videoView.getCurrentPosition();

????}

?

????@Override

????public int getDuration() {

????????// TODO Auto-generated method stub

????????return videoView.getDuration();

????}

?

????@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 postion) {

????????// TODO Auto-generated method stub

????????videoView.seekTo(postion);

????}

?

????@Override

????public void start() {

????????// TODO Auto-generated method stub

????????videoView.start();

????}

}

Android播放本地视频