Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
/ AndroidMediaCache Public archive

为 MediaPlayer 提供一个扩展,实现边放边缓存的功能。

License

Notifications You must be signed in to change notification settings

boyan01/AndroidMediaCache

Repository files navigation

AndroidStreamCache Download

为 MediaPlayer 提供一个扩展,实现边放边缓存的功能。

如何使用

  1. add gradle dependencies
implementation "tech.summerly:streamcache:$latest_version"
  1. 初始化一些变量
override fun onCreate(){
    //to init cache folder
    CacheGlobalSetting.CACHE_PATH = externalCacheDir.path
    CacheGlobalSetting.CACHE_SIZE = 800_000_000
}
  1. create a custom MediaDataSource
private class MediaDataSourceDelegate(dataSource: DataSource)
    : MediaDataSource(), DataSource by dataSource
  1. done
    val dataSource = CachedDataSource(Uri.parse(url))
    mediaPlayer.setDataSource(MediaDataSourceDelegate(dataSource))

val dataSource = CachedDataSource(uri = Uri.parse(url),
                                 cacheNameGenerator = md5NameGenerator,
                                 httpHeaderInjector = emptyHeaderInjector,
                                 cacheStrategy = LruCacheStrategy)
  • CacheNameGenerator 为指定的 url 生成一个独特的名字
  • HeaderInjector 访问 url 所附带的 http 头
  • CacheStrategy 成功缓存后的策略

for other MediaPlayer

例如 : ijkMediaPlayer

//create a custom MediaDataSource
private class MediaDataSourceDelegate(dataSource: DataSource)
    : IMediaDataSource, DataSource by dataSource

//the simple to use
val dataSource = CachedDataSource(Uri.parse(url))
mediaPlayer.setDataSource(MediaDataSourceDelegate(dataSource))

LICENSE

Apache License Version 2.0

About

为 MediaPlayer 提供一个扩展,实现边放边缓存的功能。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published