Android播放本地视频

时间:2024-02-20 20:55:21

代码如下:

publicclass MainActivity extends Activity implements MediaPlayerControl {

 

    private VideoView videoView;

    private MediaController mediaController;

 

    @Override

    protectedvoid 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

    publicboolean canPause() {

        // TODO Auto-generated method stub

        returnvideoView.canPause();

    }

 

    @Override

    publicboolean canSeekBackward() {

        // TODO Auto-generated method stub

        returnvideoView.canSeekBackward();

    }

 

    @Override

    publicboolean canSeekForward() {

        // TODO Auto-generated method stub

        returnvideoView.canSeekForward();

    }

 

    @Override

    publicint getAudioSessionId() {

        // TODO Auto-generated method stub

        return 0;

    }

 

    @Override

    publicint getBufferPercentage() {

        // TODO Auto-generated method stub

        return 0;

    }

 

    @Override

    publicint getCurrentPosition() {

        // TODO Auto-generated method stub

        returnvideoView.getCurrentPosition();

    }

 

    @Override

    publicint getDuration() {

        // TODO Auto-generated method stub

        returnvideoView.getDuration();

    }

 

    @Override

    publicboolean isPlaying() {

        // TODO Auto-generated method stub

        returnvideoView.isPlaying();

    }

 

    @Override

    publicvoid pause() {

        // TODO Auto-generated method stub

        if (videoView.isPlaying()) {

            videoView.pause();

        }

    }

 

    @Override

    publicvoid seekTo(int postion) {

        // TODO Auto-generated method stub

        videoView.seekTo(postion);

    }

 

    @Override

    publicvoid start() {

        // TODO Auto-generated method stub

        videoView.start();

    }

}