Skip to content

Commit 68d361a

Browse files
fix: playing items got wrongly removed when preparing failed
1 parent cb15888 commit 68d361a

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

android/src/main/kotlin/project/pipepipe/app/mediasource/CustomMediaSourceFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ class LazyUrlMediaSource(
227227
}
228228
} catch (e: Exception) { // can be cancelled if service get destroyed
229229
e.printStackTrace()
230-
prepareError = e
230+
// Wrap with IOException to include mediaId, which ExoPlayer can handle
231+
prepareError = java.io.IOException("Failed to prepare media: ${mediaItem.mediaId}", e)
231232
}
232233
}
233234
}

android/src/main/kotlin/project/pipepipe/app/service/PlaybackService.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -554,29 +554,37 @@ class PlaybackService : MediaLibraryService() {
554554
player.play()
555555
return
556556
}
557+
558+
// Extract failed mediaId from error message
559+
val failedMediaId = error.cause?.message?.substringAfter("Failed to prepare media: ")
560+
?: error.message?.substringAfter("Failed to prepare media: ")
561+
557562
MainScope().launch {
558563
DatabaseOperations.insertErrorLog(
559564
stacktrace = error.stackTraceToString(),
560-
request = player.currentMediaItem?.mediaId,
565+
request = failedMediaId ?: player.currentMediaItem?.mediaId,
561566
task = "PLAY_STREAM",
562567
errorCode = "PLAY_000"
563568
)
564569
}
565570
ToastManager.show(MR.strings.playback_error.desc().toString(this@PlaybackService))
566571

567-
// Remove failed item and try to play next
568-
val currentIndex = player.currentMediaItemIndex
569-
val hasNext = player.hasNextMediaItem()
570-
571-
if (currentIndex >= 0 && currentIndex < player.mediaItemCount) {
572-
player.removeMediaItem(currentIndex)
573-
574-
if (hasNext && player.mediaItemCount > 0) {
575-
// If there was a next item, it's now at the current index
576-
player.prepare()
577-
player.play()
572+
// Find and remove the failed item by mediaId
573+
if (failedMediaId != null) {
574+
for (i in 0 until player.mediaItemCount) {
575+
if (player.getMediaItemAt(i).mediaId == failedMediaId) {
576+
player.removeMediaItem(i)
577+
break
578+
}
578579
}
579580
}
581+
// If no mediaId found, don't remove anything to avoid removing wrong item
582+
583+
// Continue playback if there are items left
584+
if (player.mediaItemCount > 0) {
585+
player.prepare()
586+
player.play()
587+
}
580588
}
581589
}
582590
}

0 commit comments

Comments
 (0)