Skip to content

Commit

Permalink
增加exoplayer和sysplayer的网速显示支持
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuoSmall committed Feb 15, 2019
1 parent 234e157 commit 1b4eac6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 12 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 二、其他推荐
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -25,19 +27,26 @@

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;
}

@Override
public void initVideoPlayer(Context context, Message msg, List<VideoOptionModel> optionModelList, ICacheManager cacheManager) {
this.context = context.getApplicationContext();
mediaPlayer = new IjkExo2MediaPlayer(context);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (dummySurface == null) {
Expand Down Expand Up @@ -121,6 +130,8 @@ public void release() {
dummySurface.release();
dummySurface = null;
}
lastTotalRxBytes = 0;
lastTimeStamp = 0;
}

@Override
Expand All @@ -134,7 +145,7 @@ public int getBufferedPercentage() {
@Override
public long getNetSpeed() {
if (mediaPlayer != null) {
//todo
return getNetSpeed(context);
}
return 0;
}
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,19 +26,26 @@

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;
}

@Override
public void initVideoPlayer(Context context, Message msg, List<VideoOptionModel> optionModelList, ICacheManager cacheManager) {
this.context = context.getApplicationContext();
mediaPlayer = new AndroidMediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
release = false;
Expand Down Expand Up @@ -104,6 +112,8 @@ public void release() {
release = true;
mediaPlayer.release();
}
lastTotalRxBytes = 0;
lastTimeStamp = 0;
}

@Override
Expand All @@ -114,7 +124,7 @@ public int getBufferedPercentage() {
@Override
public long getNetSpeed() {
if (mediaPlayer != null) {
//todo
return getNetSpeed(context);
}
return 0;
}
Expand Down Expand Up @@ -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;
}
}
20 changes: 10 additions & 10 deletions gsyVideoPlayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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"

}

Expand Down

0 comments on commit 1b4eac6

Please sign in to comment.