Skip to content

Commit

Permalink
缓存模块修改
Browse files Browse the repository at this point in the history
  • Loading branch information
lizixian committed Dec 19, 2019
1 parent 171b5fe commit a001d5c
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 172 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle
Expand Up @@ -45,8 +45,6 @@ android {
//implementation 'com.lzx:StarrySkyJava:2.2.9'
// implementation 'com.google.android.exoplayer:exoplayer-hls:2.9.4'

implementation 'com.liulishuo.filedownloader:library:1.7.7'

implementation 'com.tencent.bugly:crashreport:3.1.0'
implementation 'com.tencent.bugly:nativecrashreport:3.7.1'
}
Expand Down
75 changes: 0 additions & 75 deletions app/src/main/java/com/lzx/musiclib/TestApplication.java
Expand Up @@ -2,27 +2,18 @@

import android.Manifest;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadSampleListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.lzx.musiclib.example.MusicRequest;
import com.lzx.musiclib.imageloader.GlideLoader;
import com.lzx.starrysky.StarrySky;
import com.lzx.starrysky.StarrySkyBuilder;
import com.lzx.starrysky.StarrySkyConfig;
import com.lzx.starrysky.delayaction.Valid;
import com.lzx.starrysky.playback.offline.StarrySkyCache;
import com.lzx.starrysky.playback.offline.StarrySkyCacheManager;
import com.lzx.starrysky.provider.SongInfo;
import com.lzx.starrysky.registry.StarrySkyRegistry;
import com.lzx.starrysky.utils.StarrySkyUtils;
Expand All @@ -32,10 +23,6 @@
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener;
import com.tencent.bugly.crashreport.CrashReport;

import org.jetbrains.annotations.NotNull;

import java.io.File;

/**
* create by lzx
* time:2018/11/9
Expand Down Expand Up @@ -68,68 +55,6 @@ public void applyStarrySkyRegistry(@NonNull Context context, StarrySkyRegistry r
registry.appendValidRegistry(new RequestSongInfoValid(context));
registry.registryImageLoader(new GlideLoader());
}

// @Override
// public StarrySkyCacheManager.CacheFactory getCacheFactory() {
// return new StarrySkyCacheManager.CacheFactory() {
// @NotNull
// @Override
// public StarrySkyCache build(@NotNull Context context, @NotNull StarrySkyCacheManager manager) {
// return new StarrySkyCache() {
// @Override
// public boolean isCache(@NotNull String url) {
// String path =
// manager.getDownloadDirectory(context).getAbsolutePath() + "/" +
// StarrySkyCacheManager.DOWNLOAD_CONTENT_DIRECTORY + "/111.mp3";
// File file = new File(path);
// Log.i("TestApplication", "isCache = " + file.exists());
// return file.exists();
// }
//
// @Override
// public void startCache(@NotNull String mediaId, @NotNull String url,
// @NotNull String extension) {
// if (isCache(url)) {
// return;
// }
// String path =
// manager.getDownloadDirectory(context).getAbsolutePath() + "/" +
// StarrySkyCacheManager.DOWNLOAD_CONTENT_DIRECTORY + "/111.mp3";
// FileDownloader.setup(context);
// FileDownloadLog.NEED_LOG = true;
// FileDownloader.getImpl().create(url)
// .setPath(path)
// .setListener(new FileDownloadSampleListener() {
// @Override
// protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
// Log.i("TestApplication", "progress = " + soFarBytes + "/" + totalBytes);
// }
//
// @Override
// protected void completed(BaseDownloadTask task) {
// Log.i("TestApplication", "= completed = ");
// }
//
// @Override
// protected void error(BaseDownloadTask task, Throwable e) {
// Log.i("TestApplication", "= error = " + e.getMessage());
// }
// }).start();
// }
//
// @Override
// public void deleteCacheFileByUrl(@NotNull String url) {
//
// }
//
// @Override
// public boolean deleteAllCacheFile() {
// return false;
// }
// };
// }
// };
// }
}

public static class RequestSongInfoValid implements Valid {
Expand Down
12 changes: 5 additions & 7 deletions readme/StarrySky介绍.md
Expand Up @@ -112,7 +112,11 @@ public class StarrySkyBuilder {
boolean isOpenCache;
//缓存文件夹
String cacheDestFileDir;

//超时时间设置
long httpConnectTimeout = -1;
long httpReadTimeout = -1;
//是否跳过https
boolean skipSSLChain = false;
//...
}
```
Expand Down Expand Up @@ -208,10 +212,4 @@ Playback 是播放器实现,StarrySky 内部默认的播放器实现是 ExoPla
getNotificationFactory 方法即可实现自己的通知栏,具体可参数文档 [快速集成通知栏](https://github.com/lizixian18/MusicLibrary/blob/StarrySkyJava/readme/快速集成通知栏.md)


## getCacheFactory

同样地,也是为了代码简洁清晰,分出来来这么一个方法用来自定义实现媒体缓存,缓存的结构跟通知栏差不多,也是通过工厂类构建的,重写 getCacheFactory
即可实现自己的缓存功能,具体可参考文档 [媒体缓存功能](https://github.com/lizixian18/MusicLibrary/blob/StarrySkyJava/readme/媒体缓存功能.md)


StarrySky 的配置讲完了,可以看到 StarrySky 完全支持用户自定义实现各种组件,具有良好的扩展性。更多功能自己探索哦。
5 changes: 4 additions & 1 deletion readme/StarrySky各种API功能.md
Expand Up @@ -11,11 +11,14 @@
- 如果想要添加的字段跟媒体信息有关,可以留言给我添加上去。


**SongInfo中必须要有的字段**
**SongInfo 中必须要有的字段**

在实现播放功能时,SongInfo 有两个值必须要填,那就是 songId 和 songUrl,否则无法播放。其中 songId 是媒体 Id,你可以随便给他赋值,只需要保证唯一就行,
因为它是每个媒体的唯一标记,songUrl 就是媒体的播放地址。

**设置请求头**
有些音频需要添加请求 header 才能访问,所以 SongInfo 中 有一个 mMapHeadData 字段,它可以设置请求头。


## PlayerControl 播放控制

Expand Down
78 changes: 3 additions & 75 deletions readme/媒体缓存功能.md
@@ -1,10 +1,9 @@
# 媒体缓存功能

<img src="https://raw.githubusercontent.com/lizixian18/MusicLibrary/StarrySkyJava/art/Cache.png">

媒体缓存功能,也就是传说中的边播边存,它的结构图如上所示,跟通知栏类似,StarrySky 的媒体缓存是由 StarrySkyCacheManager
管理,通过 CacheFactory 去创建的。内部默认实现了一个缓存功能 ExoCache,它是基于 ExoPlayer 自带的媒体下载功能实现的。
同样地,如果你不想使用默认的缓存实现,也可以自己去实现缓存功能。
媒体缓存功能,也就是传说中的边播边存,StarrySky 由于默认使用 ExoPlayer,所以缓存使用的是 ExoPlayer 的 SimpleCache。
如果要自定义缓存实现,请使用自定播放器功能自行定制。缓存管理类是 StarrySkyCacheManager


## 打开缓存开关

Expand Down Expand Up @@ -51,77 +50,6 @@ private static class TestConfig extends StarrySkyConfig {

通过 setCacheDestFileDir 方法即可配置缓存文件夹。

## 自定义缓存实现

自定义缓存实现需要重写 getCacheFactory 方法,这个方法是为了代码清晰而特意分出来的:

```java
private static class TestConfig extends StarrySkyConfig {

@Override
public void applyOptions(@NonNull Context context, @NonNull StarrySkyBuilder builder) {
super.applyOptions(context, builder);
builder.setOpenCache(true);

String destFileDir = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/111StarrySkyCache/";
builder.setCacheDestFileDir(destFileDir);
}

@Override
public StarrySkyCacheManager.CacheFactory getCacheFactory() {
return super.getCacheFactory();
}
}
```

因为缓存是通过 CacheFactory 创建出来的,所以这里需要实现自己的 CacheFactory,然后返回即可:

```java
@Override
public StarrySkyCacheManager.CacheFactory getCacheFactory() {
return new MyCacheFactory();
}

public static class MyCacheFactory implements StarrySkyCacheManager.CacheFactory {

@NonNull
@Override
public StarrySkyCache build(Context context, StarrySkyCacheManager manager) {
return null;
}
}
```

CacheFactory 需要实现 build 方法,返回的是一个 StarrySkyCache 对象,下面看看实现 StarrySkyCache 需要实现那些方法:

```java
public interface StarrySkyCache {

/**
* 根据url判断是否已经缓存
*/
boolean isCache(String url);

/**
* 开始缓存
*/
void startCache(String mediaId, String url, String extension);

/**
* 根据url删除指定缓存文件
*/
void deleteCacheFileByUrl(String url);

/**
* 删除所有缓存文件
*/
boolean deleteAllCacheFile();
}
```

只需要实现上述的方法,即可实现自己的缓存功能。


## 权限申请

Expand Down
6 changes: 1 addition & 5 deletions starrysky/src/main/java/com/lzx/starrysky/StarrySky.java
Expand Up @@ -133,14 +133,10 @@ private static void initializeStarrySky(Context context, StarrySkyBuilder builde
new StarrySkyNotificationManager(builder.isOpenNotification, factory);
starrySky.mRegistry.registryNotificationManager(notificationManager);

//注册缓存
StarrySkyCacheManager.CacheFactory cacheFactory =
mStarrySkyConfig != null ? mStarrySkyConfig.getCacheFactory() : null;
StarrySkyCacheManager cacheManager = new StarrySkyCacheManager(
context,
builder.isOpenCache,
builder.cacheDestFileDir,
cacheFactory);
builder.cacheDestFileDir);
starrySky.mRegistry.registryStarryCache(cacheManager);

//播放器
Expand Down
Expand Up @@ -30,11 +30,4 @@ public void applyStarrySkyRegistry(@NonNull Context context, StarrySkyRegistry r
public StarrySkyNotificationManager.NotificationFactory getNotificationFactory() {
return null;
}

/**
* 缓存配置
*/
public StarrySkyCacheManager.CacheFactory getCacheFactory() {
return null;
}
}

0 comments on commit a001d5c

Please sign in to comment.