From 1b4eac6fff04b84540d953402fcb5a24bec38cb4 Mon Sep 17 00:00:00 2001 From: guoshuyu Date: Fri, 15 Feb 2019 10:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0exoplayer=E5=92=8Csysplayer?= =?UTF-8?q?=E7=9A=84=E7=BD=91=E9=80=9F=E6=98=BE=E7=A4=BA=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 55 +++++++++++++++++++ .../ijk/media/exo2/Exo2PlayerManager.java | 32 ++++++++++- .../player/SystemPlayerManager.java | 29 +++++++++- gsyVideoPlayer/build.gradle | 20 +++---- 4 files changed, 124 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e58357794..580f39631 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,61 @@ implementation 'com.shuyu:gsyVideoPlayer-ex_so:6.0.3' ``` +#### D、代码中的全局切换支持(更多请参看下方文档和demo) + +``` + +//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; + + + +//切换绘制模式 +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; + } +}); + +``` + ### [--- 更多依赖方式请点击 - ](https://github.com/CarGuo/GSYVideoPlayer/blob/master/doc/DEPENDENCIES.md) ## 二、其他推荐 diff --git a/gsyVideoPlayer-exo_player2/src/main/java/tv/danmaku/ijk/media/exo2/Exo2PlayerManager.java b/gsyVideoPlayer-exo_player2/src/main/java/tv/danmaku/ijk/media/exo2/Exo2PlayerManager.java index 2119dd5fa..934ddb93b 100644 --- a/gsyVideoPlayer-exo_player2/src/main/java/tv/danmaku/ijk/media/exo2/Exo2PlayerManager.java +++ b/gsyVideoPlayer-exo_player2/src/main/java/tv/danmaku/ijk/media/exo2/Exo2PlayerManager.java @@ -2,6 +2,7 @@ import android.content.Context; import android.media.AudioManager; +import android.net.TrafficStats; import android.net.Uri; import android.os.Handler; import android.os.Looper; @@ -14,6 +15,7 @@ import com.shuyu.gsyvideoplayer.model.VideoOptionModel; import com.shuyu.gsyvideoplayer.player.IPlayerManager; +import java.text.DecimalFormat; import java.util.List; import tv.danmaku.ijk.media.player.IMediaPlayer; @@ -25,12 +27,18 @@ public class Exo2PlayerManager implements IPlayerManager { + private Context context; + private IjkExo2MediaPlayer mediaPlayer; private Surface surface; private DummySurface dummySurface; + private long lastTotalRxBytes = 0; + + private long lastTimeStamp = 0; + @Override public IMediaPlayer getMediaPlayer() { return mediaPlayer; @@ -38,6 +46,7 @@ public IMediaPlayer getMediaPlayer() { @Override public void initVideoPlayer(Context context, Message msg, List optionModelList, ICacheManager cacheManager) { + this.context = context.getApplicationContext(); mediaPlayer = new IjkExo2MediaPlayer(context); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); if (dummySurface == null) { @@ -121,6 +130,8 @@ public void release() { dummySurface.release(); dummySurface = null; } + lastTotalRxBytes = 0; + lastTimeStamp = 0; } @Override @@ -134,7 +145,7 @@ public int getBufferedPercentage() { @Override public long getNetSpeed() { if (mediaPlayer != null) { - //todo + return getNetSpeed(context); } return 0; } @@ -234,4 +245,23 @@ public int getVideoSarDen() { public boolean isSurfaceSupportLockCanvas() { return false; } + + + private long getNetSpeed(Context context) { + if (context == null) { + return 0; + } + long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB + long nowTimeStamp = System.currentTimeMillis(); + long calculationTime = (nowTimeStamp - lastTimeStamp); + if (calculationTime == 0) { + return calculationTime; + } + //毫秒转换 + long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / calculationTime); + lastTimeStamp = nowTimeStamp; + lastTotalRxBytes = nowTotalRxBytes; + return speed; + } + } diff --git a/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/player/SystemPlayerManager.java b/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/player/SystemPlayerManager.java index 7fd9f4504..40bf138eb 100644 --- a/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/player/SystemPlayerManager.java +++ b/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/player/SystemPlayerManager.java @@ -3,6 +3,7 @@ import android.content.Context; import android.media.AudioManager; import android.media.PlaybackParams; +import android.net.TrafficStats; import android.net.Uri; import android.os.Build; import android.os.Message; @@ -25,12 +26,18 @@ public class SystemPlayerManager implements IPlayerManager { + private Context context; + private AndroidMediaPlayer mediaPlayer; private Surface surface; private boolean release; + private long lastTotalRxBytes = 0; + + private long lastTimeStamp = 0; + @Override public IMediaPlayer getMediaPlayer() { return mediaPlayer; @@ -38,6 +45,7 @@ public IMediaPlayer getMediaPlayer() { @Override public void initVideoPlayer(Context context, Message msg, List optionModelList, ICacheManager cacheManager) { + this.context = context.getApplicationContext(); mediaPlayer = new AndroidMediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); release = false; @@ -104,6 +112,8 @@ public void release() { release = true; mediaPlayer.release(); } + lastTotalRxBytes = 0; + lastTimeStamp = 0; } @Override @@ -114,7 +124,7 @@ public int getBufferedPercentage() { @Override public long getNetSpeed() { if (mediaPlayer != null) { - //todo + return getNetSpeed(context); } return 0; } @@ -232,4 +242,21 @@ private void setSpeed(float speed) { } } } + + private long getNetSpeed(Context context) { + if (context == null) { + return 0; + } + long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB + long nowTimeStamp = System.currentTimeMillis(); + long calculationTime = (nowTimeStamp - lastTimeStamp); + if (calculationTime == 0) { + return calculationTime; + } + //毫秒转换 + long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / calculationTime); + lastTimeStamp = nowTimeStamp; + lastTotalRxBytes = nowTotalRxBytes; + return speed; + } } diff --git a/gsyVideoPlayer/build.gradle b/gsyVideoPlayer/build.gradle index 9da0cab07..017094818 100644 --- a/gsyVideoPlayer/build.gradle +++ b/gsyVideoPlayer/build.gradle @@ -35,8 +35,8 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - //api project(':gsyVideoPlayer-java') - //api project(':gsyVideoPlayer-exo_player2') + api project(':gsyVideoPlayer-java') + api project(':gsyVideoPlayer-exo_player2') //api project(':gsyVideoPlayer-armv5') //api project(':gsyVideoPlayer-armv7a') //api project(':gsyVideoPlayer-armv64') @@ -48,16 +48,16 @@ dependencies { //api "com.shuyu:GSYVideoPlayer:$gsyVideoVersion" - api "com.shuyu:gsyVideoPlayer-java:$gsyVideoVersion" - api "com.shuyu:GSYVideoPlayer-exo2:$gsyVideoVersion" - api "com.shuyu:gsyVideoPlayer-armv5:$gsyVideoVersion" - api "com.shuyu:gsyVideoPlayer-armv7a:$gsyVideoVersion" - api "com.shuyu:gsyVideoPlayer-arm64:$gsyVideoVersion" - api "com.shuyu:gsyVideoPlayer-x64:$gsyVideoVersion" - api "com.shuyu:gsyVideoPlayer-x86:$gsyVideoVersion" + //api "com.shuyu:gsyVideoPlayer-java:$gsyVideoVersion" + //api "com.shuyu:GSYVideoPlayer-exo2:$gsyVideoVersion" + //api "com.shuyu:gsyVideoPlayer-armv5:$gsyVideoVersion" + //api "com.shuyu:gsyVideoPlayer-armv7a:$gsyVideoVersion" + //api "com.shuyu:gsyVideoPlayer-arm64:$gsyVideoVersion" + //api "com.shuyu:gsyVideoPlayer-x64:$gsyVideoVersion" + //api "com.shuyu:gsyVideoPlayer-x86:$gsyVideoVersion" //更多配置版so,增加了concat,rtsp,mpeg,crypto - //api "com.shuyu:gsyVideoPlayer-ex_so:$gsyVideoVersion" + api "com.shuyu:gsyVideoPlayer-ex_so:$gsyVideoVersion" }