软件编程
位置:首页>> 软件编程>> Android编程>> Android GSYVideoPlayer视频播放器功能的实现

Android GSYVideoPlayer视频播放器功能的实现

作者:Android_零零七  发布时间:2022-01-12 23:57:08 

标签:Android,GSYVideoPlayer,视频播放器

GSYVideoPlayer

GSYVideoPlayer官方地址

GSYVideoPlayer 一个基于IJkPlayer的播放器
支持调节声音亮度
边播边缓存 使用了AndroidVideoCache;ExoPlayer使用SimpleCache
支持多种协议h263\4\5、Https、concat、rtsp、hls、rtmp、crypto、mpeg等等
简单滤镜(马赛克、黑白、色彩过滤、高斯、模糊、模糊等等20多种)、动画、(水印、画面多重播放等)
视频第一帧、视频帧截图功能,视频生成gif功能。
调整显示比例:默认、16:9、4:3、填充;播放时旋转画面角度(0,90,180,270);镜像旋转
IJKPlayer、EXOPlayer、MediaPlayer切换、自定义内核
小窗口、多窗体下(包括桌面)的小窗口播放。
片头广告、跳过广告支持、中间插入广告功能。
暂停前后台切换不黑屏;调整不同清晰度的支持;无缝切换支持;锁定/解锁全屏点击功能;进度条小窗口预览
可自定义渲染层、自定义管理层、自定义播放层(控制层)、自定义缓存层

使用依赖

1、JCenter 引入方法(推荐)
你可以选择下面三种的其中一种,在module下的build.gradle添加。

A、直接引入


//完整版引入
implementation 'com.shuyu:GSYVideoPlayer:8.1.2'

B、添加java和你想要的so支持:


implementation 'com.shuyu:gsyVideoPlayer-java:8.1.2'

//是否需要ExoPlayer模式
implementation 'com.shuyu:GSYVideoPlayer-exo2:8.1.2'

//根据你的需求ijk模式的so
implementation 'com.shuyu:gsyVideoPlayer-armv5:8.1.2'
implementation 'com.shuyu:gsyVideoPlayer-armv7a:8.1.2'
implementation 'com.shuyu:gsyVideoPlayer-arm64:8.1.2'
implementation 'com.shuyu:gsyVideoPlayer-x64:8.1.2'
implementation 'com.shuyu:gsyVideoPlayer-x86:8.1.2'

C、支持其他格式协议的(mpeg,rtsp, concat、crypto协议)


implementation 'com.shuyu:gsyVideoPlayer-java:8.1.2'

//是否需要ExoPlayer模式
implementation 'com.shuyu:GSYVideoPlayer-exo2:8.1.2'

//更多ijk的编码支持
implementation 'com.shuyu:gsyVideoPlayer-ex_so:8.1.2'

代码中的全局切换支持


//EXOPlayer内核,支持格式更多
PlayerFactory.setPlayManager(Exo2PlayerManager.class);
//系统内核模式
PlayerFactory.setPlayManager(SystemPlayerManager.class);
//ijk内核,默认模式
PlayerFactory.setPlayManager(IjkPlayerManager.class);

//exo缓存模式,支持m3u8,只支持exo
CacheFactory.setCacheManager(ExoPlayerCacheManager.class);
//代理缓存模式,支持所有模式,不支持m3u8等,默认
CacheFactory.setCacheManager(ProxyCacheManager.class);

//切换渲染模式
GSYVideoType.setShowType(GSYVideoType.SCREEN_MATCH_FULL);
//默认显示比例
GSYVideoType.SCREEN_TYPE_DEFAULT = 0;
//16:9
GSYVideoType.SCREEN_TYPE_16_9 = 1;
//4:3
GSYVideoType.SCREEN_TYPE_4_3 = 2;
//全屏裁减显示,为了显示正常 CoverImageView 建议使用FrameLayout作为父布局
GSYVideoType.SCREEN_TYPE_FULL = 4;
//全屏拉伸显示,使用这个属性时,surface_container建议使用FrameLayout
GSYVideoType.SCREEN_MATCH_FULL = -4;
/***
* SCREEN_TYPE_CUSTOM 下自定义显示比例
* @param screenScaleRatio 高宽比,如 16:9
*/
public static void setScreenScaleRatio(float screenScaleRatio)

//切换绘制模式
GSYVideoType.setRenderType(GSYVideoType.SUFRACE);
GSYVideoType.setRenderType(GSYVideoType.GLSURFACE);
GSYVideoType.setRenderType(GSYVideoType.TEXTURE);

//ijk关闭log
IjkPlayerManager.setLogLevel(IjkMediaPlayer.IJK_LOG_SILENT);

//exoplayer自定义MediaSource
ExoSourceManager.setExoMediaSourceInterceptListener(new ExoMediaSourceInterceptListener() {
 @Override
 public MediaSource getMediaSource(String dataSource, boolean preview, boolean cacheEnable, boolean isLooping, File cacheDir) {
   //可自定义MediaSource
   return null;
 }
});

自定义一个播放器


public class LiveVideo extends StandardGSYVideoPlayer {
 public LiveDataFullscreenButtonClick liveDataClick;//点击全屏按钮回调

/**
  * 恢复暂停状态
  */
 public void onResume() {
   onVideoResume();
 }
 /**
  * 暂停状态
  */
 public void onPause() {
   onVideoPause();
 }

/**
  * 接口回调
  * @param liveDataClick
  */
 public void setOnFullscreenButtonClick(LiveDataFullscreenButtonClick liveDataClick) {
   this.liveDataClick = liveDataClick;
 }

/* 重写方法自定义layout id与video_layout_standard.xml一致 不重新使用系统默认布局*/
 @Override
 public int getLayoutId() {
   return R.layout.test;
 }

public LiveVideo(Context context, Boolean fullFlag) {
   super(context, fullFlag);
   init();
 }

public LiveVideo(Context context) {
   super(context);
   init();
 }

public LiveVideo(Context context, AttributeSet attrs) {
   super(context, attrs);
   init();
 }

/* 初始化操作 */
 private void init() {
   //EXOPlayer内核,支持格式更多
//    PlayerFactory.setPlayManager(Exo2PlayerManager.class);
   //代理缓存模式,支持所有模式,不支持m3u8等,默认
//    CacheFactory.setCacheManager(ProxyCacheManager.class);
   //系统内核模式
//    PlayerFactory.setPlayManager(SystemPlayerManager.class);
   //ijk内核,默认模式
   PlayerFactory.setPlayManager(IjkPlayerManager.class);
   settingsVideo();
 }
 /* 一些播放器的设置 做一些UI的隐藏 可根据自己需求*/
 public void settingsVideo() {
   GSYVideoType.enableMediaCodec();//使能硬解码,播放前设置
   Debuger.enable();//打开GSY的Log
   //隐藏一些UI
   setViewShowState(mBottomContainer, VISIBLE);
   setViewShowState(mTopContainer, VISIBLE);
   setViewShowState(mLockScreen, GONE);
   setViewShowState(mLoadingProgressBar, GONE);
   setViewShowState(mTopContainer, GONE);
   setViewShowState(mThumbImageView, GONE);
   setViewShowState(mBottomProgressBar, GONE);
   //显示一些UI  进度 时间 当前时间 全屏 返回 加载Loading 暂停开始
   setViewShowState(mStartButton, VISIBLE);
   setViewShowState(mLoadingProgressBar, VISIBLE);
   setViewShowState(mFullscreenButton, VISIBLE);
   setViewShowState(mBackButton, GONE);
   setViewShowState(mProgressBar, VISIBLE);
   setViewShowState(mCurrentTimeTextView, VISIBLE);
   setViewShowState(mTotalTimeTextView, VISIBLE);
   setEnlargeImageRes(R.drawable.full);
   setShrinkImageRes(R.drawable.full);
 }

//拦截事件
 @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
   mFullscreenButton.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View view) {
       liveDataClick.onClick();
     }
   });
   return super.dispatchTouchEvent(ev);
 }

/* 取消 双击暂停 */
 @Override
 protected void touchDoubleUp() {
//    super.touchDoubleUp();
 }

public interface LiveDataFullscreenButtonClick {
   void onClick();
 }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/black">
 <RelativeLayout
   android:id="@+id/surface_container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center">
 </RelativeLayout>
 <RelativeLayout
   android:id="@+id/thumb"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_alignParentBottom="true"
   android:layout_alignParentEnd="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentRight="true"
   android:layout_alignParentStart="true"
   android:layout_alignParentTop="true"
   android:background="#000000"
   android:scaleType="fitCenter" />
 <LinearLayout
   android:id="@+id/layout_bottom"
   android:layout_width="match_parent"
   android:layout_height="40dp"
   android:layout_alignParentBottom="true"
   android:background="#99000000"
   android:gravity="center_vertical"
   android:orientation="horizontal"
   android:visibility="invisible">
   <TextView
     android:id="@+id/current"
     android:textColor="@color/white"
     style="@style/news_des_style"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="16dp"
     android:text="00:00"
   />

<SeekBar
     android:id="@+id/progress"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_gravity="center_vertical"
     android:layout_weight="1.0"
     android:background="@null"
     android:padding="10dp"
     android:max="100"
     android:maxHeight="4dp"
     android:minHeight="4dp"
     android:paddingBottom="8dp"
     android:paddingTop="8dp"
     android:progressDrawable="@drawable/video_progress_bg"
     android:thumb="@drawable/video_seek_thumb" />
   <TextView
     android:id="@+id/total"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginRight="16dp"
     android:text="00:00"
     android:textColor="@color/white"
     style="@style/news_des_style"/>
 </LinearLayout>
 <LinearLayout
   android:id="@+id/layout_top"
   android:layout_width="match_parent"
   android:layout_height="48dp"
   android:background="@drawable/video_title_bg"
   android:gravity="center_vertical">
   <ImageView
     android:id="@+id/back"
     android:layout_width="48dp"
     android:layout_height="48dp"
     android:paddingLeft="10dp"
     android:scaleType="centerInside"
     android:src="@drawable/video_back" />
     <View
       android:layout_weight="1"
       android:layout_width="0dp"
       android:layout_height="match_parent"/>
   <ImageView
     android:id="@+id/fullscreen"
     style="@style/left_icon_style"
     android:src="@drawable/full" />
 </LinearLayout>
 <ProgressBar
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/loading"
   android:layout_centerInParent="true"
   />
 <ImageView
   android:visibility="gone"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:layout_centerInParent="true"
   android:padding="5dp"
   android:id="@+id/start"
  />
</RelativeLayout>

api


/**
  * 设置播放URL
  *
  * @param url      播放url
  * @param cacheWithPlay 是否边播边缓存
  * @param title     title
  * @return
  */
video.setUp("url", false, "");//设置播放路径
video.startPlayLogic();//开始播放

Android GSYVideoPlayer视频播放器功能的实现
Android GSYVideoPlayer视频播放器功能的实现

来源:https://blog.csdn.net/weixin_45680654/article/details/115325508

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com