Skip to content

Commit

Permalink
修复起搏点在特殊场景下失效问题,升级exo版本
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajunhui committed Sep 18, 2020
1 parent e94c735 commit ca90af4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 64 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ demo示例集成了播放控制组件**ControllerCover**、加载中组件**Load
dependencies {
//---------如果仅使用MediaPlayer解码,使用以下依赖。----------
//该依赖仅包含MediaPlayer解码
implementation 'com.kk.taurus.playerbase:playerbase:3.3.9'
implementation 'com.kk.taurus.playerbase:playerbase:3.4.0'
//---------如果使用ExoPlayer解码,使用以下依赖。---------
//该依赖包含exoplayer解码和MediaPlayer解码
//注意exoplayer的最小支持SDK版本为16
implementation 'cn.jiajunhui:exoplayer:339_2118_015'
implementation 'cn.jiajunhui:exoplayer:340_2120_016'
//---------如果使用ijkPlayer解码,使用以下依赖。---------
//该依赖包含ijkplayer解码和MediaPlayer解码
implementation 'cn.jiajunhui:ijkplayer:339_088_010'
implementation 'cn.jiajunhui:ijkplayer:340_088_011'
//ijk官方的解码库依赖,较少格式版本且不支持HTTPS。
implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
# Other ABIs: optional
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ project.ext {
lib = [
//module versions
ijksdkVersion = '0.8.8',
exoplayersdkVersion = '2.11.8',
exoplayersdkVersion = '2.12.0',
appcompatVersion = '1.2.0',

playerbaseIjkVersion = '339_088_010',
playerbaseExoVersion = '339_2118_015',
playerbaseVersion = '3.3.9'
playerbaseIjkVersion = '340_088_011',
playerbaseExoVersion = '340_2120_016',
playerbaseVersion = '3.4.0'
]

}
82 changes: 46 additions & 36 deletions exoplayer/src/main/java/com/kk/taurus/exoplayer/ExoMediaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.RenderersFactory;
Expand All @@ -48,6 +49,7 @@
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.RawResourceDataSource;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoListener;
import com.kk.taurus.playerbase.config.AppContextAttach;
Expand All @@ -66,6 +68,8 @@

import java.util.HashMap;

import static com.google.android.exoplayer2.util.Assertions.checkNotNull;

public class ExoMediaPlayer extends BaseInternalPlayer {

private final String TAG = "ExoMediaPlayer";
Expand Down Expand Up @@ -181,15 +185,17 @@ public void setDataSource(DataSource dataSource) {
//handle timed text source
TimedTextSource timedTextSource = dataSource.getTimedTextSource();
if(timedTextSource!=null){
Format format = Format.createTextSampleFormat(null, timedTextSource.getMimeType(), timedTextSource.getFlag(), null);
Format format = new Format.Builder().setSampleMimeType(timedTextSource.getMimeType()).setSelectionFlags(timedTextSource.getFlag()).build();
MediaSource timedTextMediaSource = new SingleSampleMediaSource.Factory(new DefaultDataSourceFactory(mAppContext,
userAgent))
.createMediaSource(Uri.parse(timedTextSource.getPath()), format, C.TIME_UNSET);
userAgent)).createMediaSource(new MediaItem.Subtitle(
Uri.parse(timedTextSource.getPath()),
checkNotNull(format.sampleMimeType), format.language, format.selectionFlags), C.TIME_UNSET);
//merge MediaSource and timedTextMediaSource.
mediaSource = new MergingMediaSource(mediaSource, timedTextMediaSource);
}

mInternalPlayer.prepare(mediaSource);
mInternalPlayer.setMediaSource(mediaSource);
mInternalPlayer.prepare();
mInternalPlayer.setPlayWhenReady(false);

Bundle sourceBundle = BundlePool.obtain();
Expand All @@ -200,17 +206,21 @@ public void setDataSource(DataSource dataSource) {

private MediaSource getMediaSource(Uri uri, com.google.android.exoplayer2.upstream.DataSource.Factory dataSourceFactory){
int contentType = Util.inferContentType(uri);
MediaItem mediaItem = new MediaItem.Builder()
.setUri(uri)
.setMimeType(MimeTypes.APPLICATION_MPD)
.build();
switch (contentType) {
case C.TYPE_DASH:
return new DashMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
return new DashMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);
case C.TYPE_SS:
return new SsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
return new SsMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
return new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);
case C.TYPE_OTHER:
default:
// This is the MediaSource representing the media to be played.
return new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
return new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);
}
}

Expand Down Expand Up @@ -290,8 +300,13 @@ public void start() {

@Override
public void start(int msc) {
mStartPos = msc;
start();
if(getState() == STATE_PREPARED && msc > 0){
start();
seekTo(msc);
}else{
mStartPos = msc;
start();
}
}

@Override
Expand Down Expand Up @@ -387,7 +402,7 @@ public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray tra
}

@Override
public void onLoadingChanged(boolean isLoading) {
public void onIsLoadingChanged(boolean isLoading) {
int bufferPercentage = mInternalPlayer.getBufferedPercentage();
if(!isLoading){
submitBufferingUpdate(bufferPercentage, null);
Expand All @@ -396,25 +411,8 @@ public void onLoadingChanged(boolean isLoading) {
}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
PLog.d(TAG,"onPlayerStateChanged : playWhenReady = "+ playWhenReady
+ ", playbackState = " + playbackState);

if(!isPreparing){
if(playWhenReady){
if(getState()==STATE_PREPARED){
updateStatus(IPlayer.STATE_STARTED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_AUDIO_RENDER_START, null);
}else{
updateStatus(IPlayer.STATE_STARTED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_RESUME, null);
}
}else{
updateStatus(IPlayer.STATE_PAUSED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PAUSE, null);
}
}

public void onPlaybackStateChanged(int playbackState) {
PLog.d(TAG,"onPlayerStateChanged : playbackState = " + playbackState);
if(isPreparing){
switch (playbackState){
case Player.STATE_READY:
Expand All @@ -428,19 +426,13 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
updateStatus(IPlayer.STATE_PREPARED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED, bundle);

if(playWhenReady){
updateStatus(STATE_STARTED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_AUDIO_RENDER_START, null);
}

if(mStartPos > 0 && mInternalPlayer.getDuration() > 0){
mInternalPlayer.seekTo(mStartPos);
mStartPos = -1;
}
break;
}
}

if(isBuffering){
switch (playbackState){
case Player.STATE_READY:
Expand Down Expand Up @@ -480,7 +472,25 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
break;
}
}
}

@Override
public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
PLog.d(TAG,"onPlayerStateChanged : playWhenReady = "+ playWhenReady + ", reason = " + reason);
if(!isPreparing){
if(playWhenReady){
if(getState()==STATE_PREPARED){
updateStatus(IPlayer.STATE_STARTED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_AUDIO_RENDER_START, null);
}else{
updateStatus(IPlayer.STATE_STARTED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_RESUME, null);
}
}else{
updateStatus(IPlayer.STATE_PAUSED);
submitPlayerEvent(OnPlayerEventListener.PLAYER_EVENT_ON_PAUSE, null);
}
}
}

@Override
Expand Down
46 changes: 32 additions & 14 deletions ijkplayer/src/main/java/com/kk/taurus/ijkplayer/IjkPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.Surface;
import android.view.SurfaceHolder;

Expand Down Expand Up @@ -40,10 +41,12 @@ public class IjkPlayer extends BaseInternalPlayer {

private IjkMediaPlayer mMediaPlayer;

private int mTargetState;
private int mTargetState = Integer.MAX_VALUE;

private int startSeekPos;

private SparseArray<Bundle> mOptionArrays;

public static void init(Context context){
PlayerConfig.addDecoderPlan(new DecoderPlan(
PLAN_ID,
Expand All @@ -55,7 +58,8 @@ public static void init(Context context){

public IjkPlayer() {
// init player
mMediaPlayer = createPlayer();
mMediaPlayer = new IjkMediaPlayer();
mOptionArrays = new SparseArray<>();
}

static {
Expand All @@ -74,17 +78,21 @@ public IjkPlayer() {
public void option(int code, Bundle bundle) {
super.option(code, bundle);
if(bundle!=null){
Set<String> keySet = bundle.keySet();
for(String key : keySet){
mMediaPlayer.setOption(code, key, bundle.getLong(key));
}
fillOption(code, bundle);
mOptionArrays.put(code, bundle);
}
}

protected IjkMediaPlayer createPlayer(){
IjkMediaPlayer ijkMediaPlayer = new IjkMediaPlayer();
// ijkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG);
private void fillOption(int code, Bundle bundle) {
Set<String> keySet = bundle.keySet();
for(String key : keySet){
mMediaPlayer.setOption(code, key, bundle.getLong(key));
}
}

private void setOptions(IjkMediaPlayer ijkMediaPlayer) {
if(ijkMediaPlayer==null)
return;
//设置清除dns cache
//IjkMediaPlayer.OPT_CATEGORY_FORMAT, "dns_cache_clear", 1

Expand All @@ -106,7 +114,10 @@ protected IjkMediaPlayer createPlayer(){
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "http-detect-range-support", 0);

ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
return ijkMediaPlayer;
int size = mOptionArrays.size();
for(int i=0;i<size;i++){
fillOption(mOptionArrays.keyAt(i), mOptionArrays.valueAt(i));
}
}

@Override
Expand All @@ -125,6 +136,8 @@ private void openVideo(DataSource dataSource) {
reset();
resetListener();
}
mTargetState = Integer.MAX_VALUE;
setOptions(mMediaPlayer);
// REMOVED: mAudioSession
mMediaPlayer.setOnPreparedListener(mPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
Expand Down Expand Up @@ -205,11 +218,16 @@ public void start() {

@Override
public void start(int msc){
if(msc > 0){
startSeekPos = msc;
}
if(available()){
if(getState()==STATE_PREPARED && msc > 0){
start();
mMediaPlayer.seekTo(msc);
}else{
if(msc > 0){
startSeekPos = msc;
}
if(available()){
start();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ private void onTimerUpdateEvent(int curr, int duration, int bufferPercentage) {
new OnPlayerEventListener() {
@Override
public void onPlayerEvent(int eventCode, Bundle bundle) {
DebugLog.onPlayEventLog(eventCode, bundle);
mTimerCounterProxy.proxyPlayEvent(eventCode, bundle);
if(eventCode==OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED){
//when prepared set volume value
Expand All @@ -255,7 +254,6 @@ public void onPlayerEvent(int eventCode, Bundle bundle) {
new OnErrorEventListener() {
@Override
public void onErrorEvent(int eventCode, Bundle bundle) {
DebugLog.onErrorEventLog(eventCode, bundle);
mTimerCounterProxy.proxyErrorEvent(eventCode, bundle);
if(isPlayRecordOpen())
mRecordProxyPlayer.onErrorEvent(eventCode, bundle);
Expand All @@ -274,12 +272,14 @@ public void onBufferingUpdate(int bufferPercentage, Bundle extra) {

//must last callback event listener , because bundle will be recycle after callback.
private void callBackPlayEventListener(int eventCode, Bundle bundle) {
DebugLog.onPlayEventLog(eventCode, bundle);
if(mOnPlayerEventListener!=null)
mOnPlayerEventListener.onPlayerEvent(eventCode, bundle);
}

//must last callback event listener , because bundle will be recycle after callback.
private void callBackErrorEventListener(int eventCode, Bundle bundle){
DebugLog.onErrorEventLog(eventCode, bundle);
if(mOnErrorEventListener!=null)
mOnErrorEventListener.onErrorEvent(eventCode, bundle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class SysMediaPlayer extends BaseInternalPlayer {

private MediaPlayer mMediaPlayer;

private int mTargetState;
private int mTargetState = Integer.MAX_VALUE;

private long mBandWidth;

Expand All @@ -75,6 +75,7 @@ public void setDataSource(DataSource dataSource) {
reset();
resetListener();
}
mTargetState = Integer.MAX_VALUE;
// REMOVED: mAudioSession
mMediaPlayer.setOnPreparedListener(mPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
Expand Down Expand Up @@ -277,11 +278,16 @@ public void start() {

@Override
public void start(int msc) {
if(available()){
if(msc > 0){
startSeekPos = msc;
}
if(getState()==STATE_PREPARED && msc > 0){
start();
mMediaPlayer.seekTo(msc);
}else{
if(available()){
if(msc > 0){
startSeekPos = msc;
}
start();
}
}
}

Expand Down

0 comments on commit ca90af4

Please sign in to comment.