Skip to content

Commit e78c42b

Browse files
committed
fix some playback issues
1 parent f39326e commit e78c42b

File tree

2 files changed

+24
-60
lines changed

2 files changed

+24
-60
lines changed

app/src/main/java/de/xikolo/download/hlsvideodownload/HlsVideoDownloadItem.kt

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {
Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package de.xikolo.download.hlsvideodownload
22

3-
import android.net.Uri
4-
import com.google.android.exoplayer2.MediaItem
5-
import com.google.android.exoplayer2.offline.StreamKey
6-
import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist
73
import de.xikolo.download.DownloadCategory
84
import de.xikolo.download.DownloadRequest
95
import de.xikolo.models.Storage
@@ -32,26 +28,4 @@ class HlsVideoDownloadRequest(
3228
override val title: String,
3329
override val showNotification: Boolean,
3430
override val category: DownloadCategory
35-
) : DownloadRequest {
36-
37-
val mediaItem = MediaItem.Builder()
38-
.setUri(Uri.parse(url))
39-
/*.setSubtitles(
40-
subtitles?.map {
41-
MediaItem.Subtitle(
42-
Uri.parse(it.value),
43-
MimeTypes.TEXT_VTT,
44-
it.key,
45-
C.SELECTION_FLAG_DEFAULT
46-
)
47-
}
48-
)*/
49-
.setStreamKeys(
50-
listOf(
51-
StreamKey(HlsMasterPlaylist.GROUP_INDEX_VARIANT, 1),
52-
StreamKey(HlsMasterPlaylist.GROUP_INDEX_AUDIO, 1),
53-
StreamKey(HlsMasterPlaylist.GROUP_INDEX_SUBTITLE, 1)
54-
)
55-
)
56-
.build()
57-
}
31+
) : DownloadRequest

0 commit comments

Comments
 (0)