提交 0284d70e 编写于 作者: S shuyu

调整exo的cache机制

上级 6d700081
......@@ -132,7 +132,7 @@ compile 'com.shuyu:gsyVideoPlayer-ex_so:4.1.3'
## 五、近期版本
### 5.0.0(2018-XX-XX)
* 调整cache机制,抽离cache管理器
* 调整cache机制,抽离cache管理器
* update ExoPlayer to 2.8.0,全面针对ExoPlayer进行优化
* 优化ExoPlayer的问题
* 增加ExoPlayer循环播放支持
......
......@@ -30,7 +30,7 @@ public class GSYApplication extends Application {
//GSYVideoType.enableMediaCodecTexture();
//GSYVideoManager.instance().setVideoType(this, GSYVideoType.IJKEXOPLAYER); //EXO 1 播放内核,弃用
//GSYVideoManager.instance().setVideoType(this, GSYVideoType.IJKEXOPLAYER2); //EXO 2 播放内核
GSYVideoManager.instance().setVideoType(this, GSYVideoType.IJKEXOPLAYER2); //EXO 2 播放内核
//GSYVideoManager.instance().setVideoType(this, GSYVideoType.SYSTEMPLAYER); //系统播放器
//GSYVideoType.setShowType(GSYVideoType.SCREEN_MATCH_FULL);
......
......@@ -98,13 +98,13 @@ public class ListNormalAdapter extends BaseAdapter {
//默认缓存路径
//使用lazy的set可以避免滑动卡的情况存在
//holder.gsyVideoPlayer.setUpLazy(url, false, null, null, "这是title");
holder.gsyVideoPlayer.setUpLazy(url, true, null, null, "这是title");
//holder.gsyVideoPlayer.setNeedShowWifiTip(false);
/************************下方为其他路径************************************/
//如果一个列表的缓存路劲都一一致
holder.gsyVideoPlayer.setUp(url, true, new File(FileUtils.getTestPath()), "这是title");
//holder.gsyVideoPlayer.setUp(url, true, new File(FileUtils.getTestPath()), "这是title");
/************************下方为其他路径************************************/
//如果一个列表里的缓存路劲不一致
......
......@@ -89,7 +89,10 @@ public class GSYExoPlayerManager implements IPlayerManager {
@Override
public void releaseSurface() {
if (surface != null) {
surface.release();
surface = null;
}
}
@Override
......@@ -104,6 +107,10 @@ public class GSYExoPlayerManager implements IPlayerManager {
}
}
@Override
public int getBufferedPercentage() {
return -1;
}
/**
* 上一集
......
......@@ -433,6 +433,15 @@ public abstract class GSYVideoBaseManager implements IMediaPlayer.OnPreparedList
clearDefaultCache(context, url);
}
@Override
public int getBufferedPercentage() {
if (playerManager != null) {
return playerManager.getBufferedPercentage();
}
return 0;
}
protected void sendMessage(Message message) {
mMediaHandler.sendMessage(message);
}
......
......@@ -46,10 +46,11 @@ public class EXO2PlayerManager implements IPlayerManager {
try {
mediaPlayer.setLooping(gsyModel.isLooping());
mediaPlayer.setCache(gsyModel.isCache());
mediaPlayer.setCacheDir(gsyModel.getCachePath());
mediaPlayer.setDataSource(context, Uri.parse(gsyModel.getUrl()), gsyModel.getMapHeadData());
//很遗憾,EXO2的setSpeed只能在播放前生效
if (gsyModel.getSpeed() != 1 && gsyModel.getSpeed() > 0) {
mediaPlayer.setSpeed(gsyModel.getSpeed(), 1);
mediaPlayer.setSpeed(gsyModel.getSpeed(), 1);
}
} catch (Exception e) {
e.printStackTrace();
......@@ -83,7 +84,7 @@ public class EXO2PlayerManager implements IPlayerManager {
@Override
public void setNeedMute(boolean needMute) {
if(mediaPlayer != null) {
if (mediaPlayer != null) {
if (needMute) {
mediaPlayer.setVolume(0, 0);
} else {
......@@ -95,7 +96,7 @@ public class EXO2PlayerManager implements IPlayerManager {
@Override
public void releaseSurface() {
if (surface!= null) {
if (surface != null) {
surface.release();
surface = null;
}
......@@ -103,7 +104,7 @@ public class EXO2PlayerManager implements IPlayerManager {
@Override
public void release() {
if(mediaPlayer != null) {
if (mediaPlayer != null) {
mediaPlayer.setSurface(null);
mediaPlayer.release();
}
......@@ -112,4 +113,12 @@ public class EXO2PlayerManager implements IPlayerManager {
dummySurface = null;
}
}
@Override
public int getBufferedPercentage() {
if (mediaPlayer != null) {
return mediaPlayer.getBufferedPercentage();
}
return 0;
}
}
......@@ -165,6 +165,11 @@ public class IJKPlayerManager implements IPlayerManager {
}
}
@Override
public int getBufferedPercentage() {
return -1;
}
private void initIJKOption(IjkMediaPlayer ijkMediaPlayer, List<VideoOptionModel> optionModelList) {
if (optionModelList != null && optionModelList.size() > 0) {
for (VideoOptionModel videoOptionModel : optionModelList) {
......
......@@ -31,4 +31,5 @@ public interface IPlayerManager {
void release();
int getBufferedPercentage();
}
......@@ -104,4 +104,9 @@ public class SystemPlayerManager implements IPlayerManager {
mediaPlayer.release();
}
}
@Override
public int getBufferedPercentage() {
return -1;
}
}
......@@ -896,6 +896,9 @@ public abstract class GSYVideoControlView extends GSYVideoView implements View.O
if (!mTouchingProgressBar) {
if (progress != 0) mProgressBar.setProgress(progress);
}
if (getGSYVideoManager().getBufferedPercentage() > 0) {
secProgress = getGSYVideoManager().getBufferedPercentage();
}
if (secProgress > 94) secProgress = 100;
if (secProgress != 0 && !getGSYVideoManager().isCacheFile()) {
mProgressBar.setSecondaryProgress(secProgress);
......
......@@ -37,6 +37,8 @@ public interface GSYVideoViewBridge {
IMediaPlayer getMediaPlayer();
int getBufferedPercentage();
void releaseMediaPlayer();
void setCurrentVideoHeight(int currentVideoHeight);
......
......@@ -50,24 +50,24 @@ public class ExoHelper {
}
public MediaSource getMediaSource(String dataSource, boolean preview, boolean cacheEnable, boolean isLooping) {
public MediaSource getMediaSource(String dataSource, boolean preview, boolean cacheEnable, boolean isLooping, File cacheDir) {
Uri contentUri = Uri.parse(dataSource);
int contentType = inferContentType(dataSource);
MediaSource mediaSource;
switch (contentType) {
case C.TYPE_SS:
mediaSource = new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(getDataSourceFactoryCache(mAppContext, dataSource, cacheEnable, preview)),
new DefaultSsChunkSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir)),
new DefaultDataSourceFactory(mAppContext, null,
getHttpDataSourceFactory(mAppContext, preview))).createMediaSource(contentUri);
break;
case C.TYPE_DASH:
mediaSource = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(getDataSourceFactoryCache(mAppContext, dataSource, cacheEnable, preview)),
mediaSource = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir)),
new DefaultDataSourceFactory(mAppContext, null,
getHttpDataSourceFactory(mAppContext, preview))).createMediaSource(contentUri);
break;
case C.TYPE_HLS:
mediaSource = new HlsMediaSource.Factory(getDataSourceFactoryCache(mAppContext, dataSource, cacheEnable, preview)).createMediaSource(contentUri);
mediaSource = new HlsMediaSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir)).createMediaSource(contentUri);
break;
case TYPE_RTMP:
RtmpDataSourceFactory rtmpDataSourceFactory = new RtmpDataSourceFactory(null);
......@@ -77,7 +77,7 @@ public class ExoHelper {
break;
case C.TYPE_OTHER:
default:
mediaSource = new ExtractorMediaSource.Factory(getDataSourceFactoryCache(mAppContext, dataSource, cacheEnable, preview))
mediaSource = new ExtractorMediaSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir))
.setExtractorsFactory(new DefaultExtractorsFactory())
.createMediaSource(contentUri);
break;
......@@ -109,9 +109,13 @@ public class ExoHelper {
/**
* 本地缓存目录
*/
public Cache getCache(Context context) {
public Cache getCache(Context context, File cacheDir) {
String dirs = context.getCacheDir().getAbsolutePath();
if (cacheDir != null) {
dirs = cacheDir.getAbsolutePath();
}
if (mCache == null) {
String path = context.getCacheDir().getAbsolutePath() + File.separator + "exo";
String path = dirs + File.separator + "exo";
boolean isLocked = SimpleCache.isCacheFolderLocked(new File(path));
if (!isLocked) {
mCache = new SimpleCache(new File(path), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100));
......@@ -135,9 +139,9 @@ public class ExoHelper {
/**
* 获取SourceFactory,是否带Cache
*/
private DataSource.Factory getDataSourceFactoryCache(Context context, String dataSource, boolean cacheEnable, boolean preview) {
private DataSource.Factory getDataSourceFactoryCache(Context context, boolean cacheEnable, boolean preview, File cacheDir) {
if (cacheEnable) {
Cache cache = getCache(context);
Cache cache = getCache(context, cacheDir);
return new CacheDataSourceFactory(cache, getDataSourceFactory(context, preview), CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
} else {
return getDataSourceFactory(context, preview);
......
/*
* Copyright (C) 2015 Bilibili
* Copyright (C) 2015 Zhang Rui <bbcallen@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tv.danmaku.ijk.media.exo2;
import android.content.Context;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.view.Surface;
......@@ -39,29 +21,17 @@ import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.analytics.AnalyticsListener;
import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.ext.rtmp.RtmpDataSourceFactory;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.LoopingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSourceEventListener;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource;
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.HashMap;
......@@ -76,8 +46,7 @@ import tv.danmaku.ijk.media.player.misc.IjkTrackInfo;
/**
* Created by guoshuyu on 2018/1/10.
* ExoPlayer2
* fork from https://github.com/DyncKathline/IJKPlayer-android
* Exo
*/
public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.EventListener, AnalyticsListener {
private static final String TAG = "IjkExo2MediaPlayer";
......@@ -99,9 +68,22 @@ public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.Ev
private boolean mIsPrepareing = true;
private boolean mIsBuffering = false;
private boolean isLooping = false;
/**
* 是否带上header
*/
private boolean isPreview = false;
/**
* 是否开启缓存
*/
private boolean isCache = false;
/**
* dataSource等的帮组类
*/
private ExoHelper mExoHelper;
/**
* 缓存目录,可以为空
*/
private File mCacheDir;
private int audioSessionId = C.AUDIO_SESSION_ID_UNSET;
......@@ -143,11 +125,6 @@ public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.Ev
}
}
@Override
public void setDataSource(Context context, Uri uri) {
mDataSource = uri.toString();
mMediaSource = mExoHelper.getMediaSource(mDataSource, isPreview, isCache, isLooping);
}
@Override
public void setDataSource(Context context, Uri uri, Map<String, String> headers) {
......@@ -163,6 +140,12 @@ public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.Ev
setDataSource(mAppContext, Uri.parse(path));
}
@Override
public void setDataSource(Context context, Uri uri) {
mDataSource = uri.toString();
mMediaSource = mExoHelper.getMediaSource(mDataSource, isPreview, isCache, isLooping, mCacheDir);
}
@Override
public void setDataSource(FileDescriptor fd) {
throw new UnsupportedOperationException("no support");
......@@ -374,6 +357,7 @@ public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.Ev
}
/**
* 是否需要带上header
* setDataSource之前生效
*
* @param preview
......@@ -391,6 +375,7 @@ public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.Ev
}
/**
* 是否开启cache
* setDataSource之前生效
*
* @param cache
......@@ -399,6 +384,20 @@ public class IjkExo2MediaPlayer extends AbstractMediaPlayer implements Player.Ev
isCache = cache;
}
public File getCacheDir() {
return mCacheDir;
}
/**
* cache文件的目录
* setDataSource之前生效
*
* @param cacheDir
*/
public void setCacheDir(File cacheDir) {
this.mCacheDir = cacheDir;
}
public MediaSource getMediaSource() {
return mMediaSource;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册