Android VideoPlayer

一、Android 视频播放的几种方法

1、VideoView 播放组件
(1)布局xml中添加VideoView组件

  

(2)AndroidManifest文件
添加访问sd卡权限
横屏 android:screenOrientation="landscape"
去掉ActionBar android:theme="@style/Theme.AppCompat.Light.NoActionBar"
(3)MainActivity.java

public class MainActivity extends Activity {
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView = findViewById(R.id.VideoView);
        videoView.setVideoPath("/mnt/shared/other/family.mp4");     //设置视频路径
        videoView.start();
        MediaController mediaController = new MediaController(MainActivity.this,true);   //设置控制器
        videoView.setMediaController(mediaController);    //用控制器初始化videoview
    }
}

2、MediaPlayer + SurfaceView
(1)视频文件ocean.mp4放置于res目录下的raw目录中,由于surfaceView的创建需要一定的时间,因此调用addCallback,在其创建完成时,播放器设置播放幕布并进行播放。
(2)布局XML




    

    

(3)MainActivity.Java

public class MainActivity extends Activity {
    private SurfaceView surfaceView;   //播放幕布
    private MediaPlayer mediaPlayer;  //播放器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = findViewById(R.id.surfaceView);
        mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.ocean);

        //surfaceView的创建需要一定的时间
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mediaPlayer.setDisplay(surfaceView.getHolder());
                mediaPlayer.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });

        findViewById(R.id.pause).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.pause();
            }
        });

        findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.start();
            }
        });
    }
}    

Android VideoPlayer_第1张图片
3、MediaPlayer + SurfaceView + MediaController
(1)布局XML
只有一个SurfaceView组件




    


(2)MainActivity.java
自己定义MediaController,相当于将MediaPlayer 的控制交给MediaController。

public class MainActivity extends Activity implements MediaController.MediaPlayerControl {
    private SurfaceView surfaceView;   //播放幕布
    private MediaPlayer mediaPlayer;
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = findViewById(R.id.surfaceView);
        mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.ocean);

        //surfaceView的创建需要一定的时间
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mediaPlayer.setDisplay(surfaceView.getHolder());
                mediaPlayer.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });

        mediaController = new MediaController(MainActivity.this);
        mediaController.setMediaPlayer(MainActivity.this);
        mediaController.setAnchorView(findViewById(R.id.root));
        mediaController.setEnabled(true);
    }

    @Override
    public void start() {
        mediaPlayer.start();
    }

    @Override
    public void pause() {
        mediaPlayer.pause();
        //显示广告、开通会员等
    }

    @Override
    public int getDuration() {
        return mediaPlayer.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(int i) {
        mediaPlayer.seekTo(i);
    }

    @Override
    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mediaController.show();  //显示mediaController

        //将触摸事件交给mediaController去处理而不是activity
        return mediaController.onTouchEvent(event);
    }
}

Android VideoPlayer_第2张图片

你可能感兴趣的:(Android,VideoPlayer)