fix: crash when download file doesn't exist

This commit is contained in:
Bnyro 2023-08-05 10:21:26 +02:00
parent 3495649669
commit f2c2834e3f
3 changed files with 9 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import com.github.libretube.util.NowPlayingNotification
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.io.path.exists
/**
* A service to play downloaded audio in the background
@ -85,9 +86,11 @@ class OfflinePlayerService : LifecycleService() {
.build()
.loadPlaybackParams(isBackgroundMode = true)
val audioItem = downloadWithItem.downloadItems.firstOrNull { it.type == FileType.AUDIO }
val audioItem = downloadWithItem.downloadItems.filter { it.path.exists() }
.firstOrNull { it.type == FileType.AUDIO }
?: // in some rare cases, video files can contain audio
downloadWithItem.downloadItems.firstOrNull { it.type == FileType.VIDEO } ?: return false
downloadWithItem.downloadItems.firstOrNull { it.type == FileType.VIDEO }
?: return false
val mediaItem = MediaItem.Builder()
.setUri(audioItem.path.toAndroidUri())

View File

@ -40,6 +40,7 @@ import com.github.libretube.util.OfflineTimeFrameReceiver
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.io.path.exists
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class OfflinePlayerActivity : BaseActivity() {
@ -133,7 +134,7 @@ class OfflinePlayerActivity : BaseActivity() {
val downloadInfo = withContext(Dispatchers.IO) {
Database.downloadDao().findById(videoId)
}
val downloadFiles = downloadInfo.downloadItems
val downloadFiles = downloadInfo.downloadItems.filter { it.path.exists() }
playerBinding.exoTitle.text = downloadInfo.download.title
playerBinding.exoTitle.isVisible = true

View File

@ -25,6 +25,7 @@ import kotlin.io.path.deleteIfExists
import kotlin.io.path.fileSize
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlin.io.path.exists
class DownloadsAdapter(
private val context: Context,
@ -50,7 +51,7 @@ class DownloadsAdapter(
videoInfo.text = download.uploadDate?.let { TextUtils.localizeDate(it) }
val downloadSize = items.sumOf { it.downloadSize }
val currentSize = items.sumOf { it.path.fileSize() }
val currentSize = items.filter { it.path.exists() }.sumOf { it.path.fileSize() }
if (downloadSize == -1L) {
progressBar.isIndeterminate = true