基于 exoplayer 的 videoview

ExoPlayerView

项目地址: JarvanMo/ExoPlayerView
简介:基于 exoplayer 的 videoview
更多: 作者    提 Bug   
标签:
videoview- exoplayer- 视频- 直播-

ExoPlayerView is a simple video view based on ExoPlayer.

中文.

brightness controller_1 fast_forward_rewind landscape portrait volume

Features

1.There are 4 modes to resize the video: 
  fit ,  fit_width , fit_height and none.
2.Process AudioFocus automatically.
3.Change its orientation by sensor automatically
4.simple gesture action supported.

Usage

Import

Add the following to your build.gradle file

compile 'com.jarvanmo:exoplayerview:1.0.7'

ExoPlayerView can play simple video directly, such as mp4,m3u8 and so on. It's easy to use. Declare ExoVideoView in your layout files:


    <com.jarvanmo.exoplayerview.ui.ExoVideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:useController="true"
        app:resizeMode="fit"
        app:orientationAuto="true"
        />

Play

play a video :

   videoView.play(mediaSource);

The ExoVideoView will create SimpleExoPlayer by itself if we play mediaSource. Actually, you can set a player by yourself;

    videoView.setPlayer(player);

We can play from a particular position too:

   videoView.play(mediaSource,where);

Note:don't forget to release ExoPlayer:

videoView.releaseSelfPlayer();

also we can give a display name:

 mediaSource.setDisplayName("LuYu YouYue");

or

 videoView.setDisplayName("LuYu YouYue");

Manage Orientation

The ExoVideoView can change its orientation by sensor automatically only when you set a not-null orientation listener:

      videoView.setOrientationListener(new ExoVideoPlaybackControlView.OrientationListener() {
            @Override
            public void onOrientationChange(@ExoVideoPlaybackControlView.SensorOrientationType int orientation) {
                if(orientation == SENSOR_PORTRAIT){
                    changeToPortrait();
                }else if(orientation == SENSOR_LANDSCAPE){
                    changeToLandscape();
                }
            }
        });

When the ExoVideoView change its orientation by itself,The ExoVideoView will callactivity.setRequestedOrientation() if the context in controller is an Activity. The fullscreen button is the same.

You can change the orientation of ExoVideoView by:

videoView.toggleControllerOrientation();

Or

videoView.setPortrait(true);

Handle Back Events

In activity :


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){

            if(videoView.isPortrait()){
               finish();
                return false;
            }else {
                videoView.toggleControllerOrientation();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

When button in controller clicked:

        videoView.setBackListener(new ExoVideoPlaybackControlView.ExoClickListener() {
            @Override
            public boolean onClick(View view, boolean isPortrait) {
                if(isPortrait){
                    finish();
                }
              return false;
            }
        });

if onClick() return true,it'll interrupt controller's operation.If it return false and you set a not-null OrientationListener,The ExoVideoView will request to change its orientation automatically.If the ExoVideoView's orientation is landscape, it'll be changed to portrait and OrientationLister.onOrientationChange() will be called.

Others

Also you can add you view to the controller view when landscape:

  videoView.addViewToControllerWhenLandscape(view);

the view you want to add will add into FrameLayout.

NOTE

Never forget to release the ExoPlayer.

 videoView.releaseSelfPlayer();

or

player.release();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值