GSYVideoPlayer 是一款基于 Android 平台的强大视频播放库,支持多种播放模式和丰富的自定义功能,适合用来快速集成视频播放功能。
最近打开了以前要实现的Nb视频(改成了lowb视频)发现哇真的是我写的吗🤣😂🙃🤪🤐🙄😵 (北纬哥还说真要这么写可以去讨饭了😂) 不说了 重构下
一、引入依赖
1
| implementation 'com.github.CarGuo.GSYVideoPlayer:gsyvideoplayer:v10.0.0'
|
二、布局文件添加播放器控件
视频控件还是简单的SampleVideo但是自定义了些功能
1 2 3 4 5 6 7 8 9 10 11 12 13
| <com.example.videoplayer.video.SampleVideo android:id="@+id/detail_player" android:layout_width="match_parent" android:layout_height="@dimen/post_media_height" tools:ignore="MissingClass" />
<ImageView android:id="@+id/next" android:layout_width="32dp" android:layout_height="fill_parent" android:paddingLeft="16dp" android:scaleType="fitCenter" android:src="@drawable/next_2" />
|
三、在 Activity/Fragment 中初始化播放器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| SampleVideo控件大部分方法基本没变.重写了下: sampleVideo.getFullscreenButton().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fullSampleVideo = (SampleVideo) sampleVideo.startWindowFullscreen(PlayerActivity.this, false, true); isFullScreen = true; fullSampleVideo.setNextEpisodeListener(new SampleVideo.OnNextEpisodeListener() { @Override public void onNextEpisode() { setNextEpisodeListener(); } }); } });
sampleVideo.setVideoAllCallBack(new SampleListener() { @Override public void onPrepared(String url, Object... objects) { super.onPrepared(url, objects); orientationUtils.setEnable(true); orientationUtils.setEnable(sampleVideo.isRotateWithSystem()); isPlay = true; }
@Override public void onAutoComplete(String url, Object... objects) { super.onAutoComplete(url, objects); }
@Override public void onQuitFullscreen(String url, Object... objects) { super.onQuitFullscreen(url, objects); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); if (orientationUtils != null) { orientationUtils.backToProtVideo(); } setSampleVideo(); }
});
@Override public boolean onTouch(View v, MotionEvent event) { gestureDetector.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (isReallyLongPress) { samSetSpeed(speed, soundTouch); isReallyLongPress = false; setIsTouchWiget(true); setIsTouchWigetFull(true); } break; } return super.onTouch(v, event); }
|
四、生命周期管理
为了避免内存泄漏,以及资源跳转失败手动控制资源释放。
1 2 3 4
| isRelease = true; if (isPlay) { getCurPlay().release(); }
|
五、其他功能
- 支持全屏播放、锁屏、手势控制等。
- 支持多种渲染模式,支持播放本地和网络视频。
- 可自定义控制面板UI。
六、官方文档和地址
温馨提示
如果你在集成过程中遇到问题,或者想了解更多高级用法,欢迎留言探讨,一起交流经验!