关于NiceVieoPlayer视频框架的使用

最近做项目的时候发现了一个超级好用的视频框架NiceVideoPlayer。文末附上GitHub地址。

NiceVieoPlayer的优点

  • 用IjkPlayer/MediaPlayer + TextureView封装,可切换IjkPlayer、MediaPlayer.
  • 支持本地和网络视频播放.
  • 完美切换小窗口、全屏,可在RecyclerView中无缝全屏.
  • 手势滑动调节播放进度、亮度、声音.
  • 支持清晰度切换.
  • 可自定义控制界面.

下面讲下如何在Studio中使用。

首先在工程的gradle中加入仓库

repositories {
  ...
  mavenCentral()
  maven { url 'https://jitpack.io' }
}

在app的gradle中引入框架


    defaultConfig {
        ...
        ndk { //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)
            abiFilters "armeabi","x86", "x86_64"
        }
    }
   //视频
   implementation 'com.github.xiaoyanger0825:NiceVieoPlayer:v2.2'

创建你将要使用视频的Activity,在对应视频界面所在的Activity的Manifest.xml中需要添加如下配置:

 

创建HomeKeyWatcher Java类用于监听Home键按下的Wathcer

public class HomeKeyWatcher {
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier mRecevier;

    public interface OnHomePressedListener {
        /**
         * 短按Home键
         */
        void onHomePressed();

        /**
         * 长按Home键
         */
//         void onHomeLongPressed();
    }

    public HomeKeyWatcher(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    /**
     * 设置监听
     *
     * @param listener
     */
    public void setOnHomePressedListener(OnHomePressedListener listener) {
        mListener = listener;
        mRecevier = new InnerRecevier();
    }

    /**
     * 开始监听,注册广播
     */
    public void startWatch() {
        if (mRecevier != null) {
            mContext.registerReceiver(mRecevier, mFilter);
        }
    }

    /**
     * 停止监听,注销广播
     */
    public void stopWatch() {
        if (mRecevier != null) {
            mContext.unregisterReceiver(mRecevier);
        }
    }

    class InnerRecevier extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    if (mListener != null) {
                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                            // 短按home键    
                            mListener.onHomePressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                            // 长按home键    
//                            mListener.onHomeLongPressed();
                        }
                    }
                }
            }
        }
    }
}  

之后就可以分为在Activity中使用以及在Fragment中使用。

Activity中使用

创建一个CompatHomeKeyActivity用于实现HomeKeyWatcher便于多个页面继承
其中BaseActivity是我自己写的一个基础Activity可以更换成AppCompatActivity;

public class CompatHomeKeyActivity extends BaseActivity {
    private boolean pressedHome;
    private HomeKeyWatcher mHomeKeyWatcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mHomeKeyWatcher = new HomeKeyWatcher(this);
        mHomeKeyWatcher.setOnHomePressedListener(new HomeKeyWatcher.OnHomePressedListener() {
            @Override
            public void onHomePressed() {
                pressedHome = true;
            }
        });
        pressedHome = false;
        mHomeKeyWatcher.startWatch();
    }

    @Override
    protected void onStop() {
        // 在OnStop中是release还是suspend播放器,需要看是不是因为按了Home键
        if (pressedHome) {
            NiceVideoPlayerManager.instance().suspendNiceVideoPlayer();
        } else {
            NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
        }
        super.onStop();
        mHomeKeyWatcher.stopWatch();
    }

    @Override
    protected void onRestart() {
        mHomeKeyWatcher.startWatch();
        pressedHome = false;
        super.onRestart();
        NiceVideoPlayerManager.instance().resumeNiceVideoPlayer();
    }

    @Override
    public void onBackPressed() {
        if (NiceVideoPlayerManager.instance().onBackPressd()) {
            return;
        }
        super.onBackPressed();
    }

}

在XMl中引用控件,因为我用RecycleView做了一个视频列表,所以这个用于Adapter中ItemVIew.



    

    

Adapter中的代码

public class VideoAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List

最后在Activity中引用,activity中就放了一个RecycleView就不贴出来了,我这边用了butterknife框架进行控件的绑定。

public class MovieActivity extends BaseActivity {
    @BindView(R.id.movie_rv)//框架的绑定
    RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        init();
    }
    private void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        VideoAdapter adapter = new VideoAdapter(this, DataUtil.getVideoListData());
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
            @Override
            public void onViewRecycled(RecyclerView.ViewHolder holder) {
                NiceVideoPlayer niceVideoPlayer = ((VideoAdapter.VideoViewHolder) holder).mVideoPlayer;
                if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
                    NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
                }
            }
        });
    }
    }
}

成品


关于NiceVieoPlayer视频框架的使用_第1张图片
这是GitHub的原址 NiceVIeoPlayer

你可能感兴趣的:(android)