提交 4be27cdd 编写于 作者: S shuyu

1.3.4 清除缓存接口

上级 9457dcdf
......@@ -45,6 +45,32 @@ compile 'com.shuyu:gsyVideoPlayer:1.3.3'
* <h4>3、详情模式</h4>
<img src="https://github.com/CarGuo/GSYVideoPlayer/blob/master/04.gif" width="240px" height="426px"/>
### 1.3.4 增加了清除默认缓存接口,优化了临时缓存文件错误的清理
GSYVideoManager
```
/**
* 删除默认所有缓存文件
*/
public static void clearAllDefaultCache(Context context)
/**
* 删除url对应默认缓存文件
*/
public static void clearDefaultCache(Context context, String url)
```
GSYVideoPlayer
```
/**
* 清除当前缓存
*/
public void clearCurrentCache()
```
### 1.3.3 优化了一些内存泄漏问题,更新了demo
......
......@@ -6,6 +6,7 @@ import android.view.View;
import android.widget.Button;
import com.example.gsyvideoplayer.utils.JumpUtils;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import butterknife.BindView;
......@@ -25,7 +26,7 @@ public class MainActivity extends AppCompatActivity {
ButterKnife.bind(this);
}
@OnClick({R.id.open_btn, R.id.list_btn, R.id.list_btn_2, R.id.list_detail})
@OnClick({R.id.open_btn, R.id.list_btn, R.id.list_btn_2, R.id.list_detail, R.id.clear_cache})
public void onClick(View view) {
switch (view.getId()) {
case R.id.open_btn:
......@@ -44,6 +45,11 @@ public class MainActivity extends AppCompatActivity {
//支持全屏重力旋转的列表播放,滑动后不会被销毁
JumpUtils.goToDetailPlayer(this);
break;
case R.id.clear_cache:
GSYVideoManager.clearAllDefaultCache(MainActivity.this);
//String url = "http://baobab.wdjcdn.com/14564977406580.mp4";
//GSYVideoManager.clearDefaultCache(MainActivity.this, url);
break;
}
}
}
......@@ -52,8 +52,8 @@ public class PlayActivity extends AppCompatActivity {
private void init() {
String url = "http://baobab.wdjcdn.com/14564977406580.mp4";
//需要路径的
videoPlayer.setUp(url, true, new File(FileUtils.getPath()), "");
//videoPlayer.setUp(url, true, "");
//videoPlayer.setUp(url, true, new File(FileUtils.getPath()), "");
videoPlayer.setUp(url, true, "");
//增加封面
ImageView imageView = new ImageView(this);
......
......@@ -38,4 +38,11 @@
android:layout_marginTop="20dp"
android:text="Detail Video2" />
<Button
android:id="@+id/clear_cache"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="clear Cache" />
</LinearLayout>
......@@ -11,8 +11,12 @@ import android.text.TextUtils;
import android.view.Surface;
import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.Md5FileNameGenerator;
import com.shuyu.gsyvideoplayer.listener.GSYMediaPlayerListener;
import com.shuyu.gsyvideoplayer.model.GSYModel;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.FileUtils;
import com.shuyu.gsyvideoplayer.utils.StorageUtils;
import java.io.File;
import java.lang.ref.WeakReference;
......@@ -76,6 +80,32 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
GSYVideoManager.instance().newProxy(context)) : proxy;
}
/**
* 删除默认所有缓存文件
*/
public static void clearAllDefaultCache(Context context) {
String path = StorageUtils.getIndividualCacheDirectory
(context.getApplicationContext()).getAbsolutePath();
FileUtils.deleteFiles(new File(path));
}
/**
* 删除url对应默认缓存文件
*/
public static void clearDefaultCache(Context context, String url) {
Md5FileNameGenerator md5FileNameGenerator = new Md5FileNameGenerator();
String name = md5FileNameGenerator.generate(url);
String pathTmp = StorageUtils.getIndividualCacheDirectory
(context.getApplicationContext()).getAbsolutePath()
+ File.separator + name + ".download";
String path = StorageUtils.getIndividualCacheDirectory
(context.getApplicationContext()).getAbsolutePath()
+ File.separator + name;
CommonUtil.deleteFile(pathTmp);
CommonUtil.deleteFile(path);
}
/**
* 获取缓存代理服务,带文件目录的
*/
......@@ -107,7 +137,6 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
}
}
/**
* 创建缓存代理服务,带文件目录的.
*/
......
......@@ -23,8 +23,10 @@ import android.widget.TextView;
import android.widget.Toast;
import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.Md5FileNameGenerator;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.StorageUtils;
import com.shuyu.gsyvideoplayer.video.GSYBaseVideoPlayer;
import java.io.File;
......@@ -715,7 +717,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View.
mTextureViewContainer.removeAllViews();
}
if (IF_FULLSCREEN_FROM_NORMAL) {//如果在进入全屏后播放完就初始化自己非全屏的控件
if (IF_FULLSCREEN_FROM_NORMAL) {
IF_FULLSCREEN_FROM_NORMAL = false;
if (GSYVideoManager.instance().lastListener() != null) {
GSYVideoManager.instance().lastListener().onAutoCompletion();
......@@ -773,16 +775,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View.
public void onError(int what, int extra) {
if (what != 38 && what != -38) {
setStateAndUi(CURRENT_STATE_ERROR);
if (mCacheFile) {
Debuger.printfError(" mCacheFile Local Error " + mUrl);
//可能是因为缓存文件除了问题
CommonUtil.deleteFile(mUrl.replace("file://", ""));
mUrl = mOriginUrl;
} else if (mUrl.contains("127.0.0.1")) {
Debuger.printfError(" mCacheFile Download Error " + mUrl);
CommonUtil.deleteFile(mUrl.replace("file://", "") + ".downlad");
mUrl = mOriginUrl;
}
deleteCacheFileWhenError();
}
}
......@@ -825,6 +818,43 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View.
}
/**
* 清除当前缓存
*/
public void clearCurrentCache() {
if (mCacheFile) {
//是否为缓存文件
Debuger.printfError(" mCacheFile Local Error " + mUrl);
//可能是因为缓存文件除了问题
CommonUtil.deleteFile(mUrl.replace("file://", ""));
mUrl = mOriginUrl;
} else if (mUrl.contains("127.0.0.1")) {
//是否为缓存了未完成的文件
Md5FileNameGenerator md5FileNameGenerator = new Md5FileNameGenerator();
String name = md5FileNameGenerator.generate(mOriginUrl);
if (mCachePath != null) {
String path = mCachePath.getAbsolutePath() + File.separator + name + ".download";
CommonUtil.deleteFile(path);
} else {
String path = StorageUtils.getIndividualCacheDirectory
(getContext().getApplicationContext()).getAbsolutePath()
+ File.separator + name + ".download";
CommonUtil.deleteFile(path);
}
}
}
/**
* 播放错误的时候,删除缓存文件
*/
private void deleteCacheFileWhenError() {
clearCurrentCache();
Debuger.printfError("Link Or mCache Error, Please Try Again" + mUrl);
mUrl = mOriginUrl;
}
protected void startProgressTimer() {
cancelProgressTimer();
UPDATE_PROGRESS_TIMER = new Timer();
......
......@@ -30,4 +30,19 @@ public class FileUtils {
return getAppPath(NAME_TEST);
}
public static void deleteFiles(File root) {
File files[] = root.listFiles();
if (files != null) {
for (File f : files) {
if (!f.isDirectory() && f.exists()) { // 判断是否存在
try {
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
\ No newline at end of file
package com.shuyu.gsyvideoplayer.utils;
import android.content.Context;
import android.os.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import static android.os.Environment.MEDIA_MOUNTED;
/**
* 拷贝了一个videocache中一样的缓存路径
*/
public class StorageUtils {
private static final Logger LOG = LoggerFactory.getLogger("StorageUtils");
private static final String INDIVIDUAL_DIR_NAME = "video-cache";
/**
* Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
* created on SD card <i>("/Android/data/[app_package_name]/cache/video-cache")</i> if card is mounted .
* Else - Android defines cache directory on device's file system.
*
* @param context Application context
* @return Cache {@link File directory}
*/
public static File getIndividualCacheDirectory(Context context) {
File cacheDir = getCacheDirectory(context, true);
return new File(cacheDir, INDIVIDUAL_DIR_NAME);
}
/**
* Returns application cache directory. Cache directory will be created on SD card
* <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
* on device's file system depending incoming parameters.
*
* @param context Application context
* @param preferExternal Whether prefer external location for cache
* @return Cache {@link File directory}.<br />
* <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
* {@link android.content.Context#getCacheDir() Context.getCacheDir()} returns null).
*/
private static File getCacheDirectory(Context context, boolean preferExternal) {
File appCacheDir = null;
String externalStorageState;
try {
externalStorageState = Environment.getExternalStorageState();
} catch (NullPointerException e) { // (sh)it happens
externalStorageState = "";
}
if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState)) {
appCacheDir = getExternalCacheDir(context);
}
if (appCacheDir == null) {
appCacheDir = context.getCacheDir();
}
if (appCacheDir == null) {
String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
appCacheDir = new File(cacheDirPath);
}
return appCacheDir;
}
private static File getExternalCacheDir(Context context) {
File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
if (!appCacheDir.exists()) {
if (!appCacheDir.mkdirs()) {
LOG.warn("Unable to create external cache directory");
return null;
}
}
return appCacheDir;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册