@@ -29,50 +29,40 @@ open class HlsVideoDownloadItem(
2929 override val title: String
3030 get() = url ? : " "
3131
32- private val cache : Cache
33- get() = if (storage == context.sdcardStorage) {
32+ private fun getCache ( storage : Storage ): Cache {
33+ return if (storage == context.sdcardStorage) {
3434 HlsVideoDownloadHandler .getSdcardStorageCache(context)!!
3535 } else {
3636 HlsVideoDownloadHandler .getInternalStorageCache(context)
3737 }
38+ }
3839
39- private val indexEntry : Download ?
40- get() = HlsVideoDownloadHandler .getManager(context, cache )
40+ private fun getIndexEntry ( storage : Storage ): Download ? {
41+ return HlsVideoDownloadHandler .getManager(context, getCache(storage) )
4142 .downloadIndex
42- .getDownload(identifier.get())
43+ .getDownload(
44+ identifier.get()
45+ )
46+ }
4347
44- private val mediaSource: MediaSource
45- get() = HlsMediaSource .Factory (
46- CacheDataSource .Factory ()
47- .setCache(cache)
48- ).createMediaSource(request.mediaItem)
48+ private fun getMediaSource (storage : Storage ): MediaSource ? {
49+ return getIndexEntry(storage)?.let {
50+ return HlsMediaSource .Factory (
51+ CacheDataSource .Factory ()
52+ .setCache(getCache(storage))
53+ .setCacheWriteDataSinkFactory(null )
54+ ).createMediaSource(
55+ it.request.toMediaItem()
56+ )
57+ }
58+ }
4959
5060 override val size: Long
51- get() = try {
52- indexEntry!! .bytesDownloaded
53- } catch (e: Exception ) {
54- 0L
55- }
61+ get() = getIndexEntry(storage)?.bytesDownloaded ? : 0L
5662
5763 final override val download: MediaSource ?
58- get() {
59- val originalStorage = storage
60- return try {
61- storage = context.internalStorage
62- indexEntry!!
63- mediaSource
64- } catch (e: Exception ) {
65- try {
66- storage = context.sdcardStorage!!
67- indexEntry!!
68- mediaSource
69- } catch (e: Exception ) {
70- null
71- }
72- } finally {
73- storage = originalStorage
74- }
75- }
64+ get() = getMediaSource(context.internalStorage)
65+ ? : context.sdcardStorage?.let { getMediaSource(it) }
7666
7767 /* val subs: Map<String, MediaSource>?
7868 get() {
0 commit comments