Skip to content

Commit f84483a

Browse files
fix: remove current media item and move to next when play error
1 parent 2aa8aad commit f84483a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.media3.exoplayer.upstream.Allocator
2121
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
2222
import androidx.media3.extractor.metadata.icy.IcyHeaders
2323
import kotlinx.coroutines.*
24+
import java.io.IOException
2425
import project.pipepipe.app.database.DatabaseOperations
2526
import project.pipepipe.shared.infoitem.StreamInfo
2627
import project.pipepipe.shared.infoitem.StreamType
@@ -162,6 +163,7 @@ class LazyUrlMediaSource(
162163
private val eventListeners = mutableMapOf<MediaSourceEventListener, Handler>()
163164
private var mediaSourceCaller: MediaSource.MediaSourceCaller? = null
164165
private val coroutineScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
166+
private var prepareError: Exception? = null
165167

166168
override fun addEventListener(
167169
handler: Handler,
@@ -225,11 +227,13 @@ class LazyUrlMediaSource(
225227
}
226228
} catch (e: Exception) { // can be cancelled if service get destroyed
227229
e.printStackTrace()
230+
prepareError = e
228231
}
229232
}
230233
}
231234

232235
override fun maybeThrowSourceInfoRefreshError() {
236+
prepareError?.let { throw it }
233237
actualMediaSource?.maybeThrowSourceInfoRefreshError()
234238
}
235239

@@ -241,7 +245,7 @@ class LazyUrlMediaSource(
241245
startPositionUs: Long
242246
): MediaPeriod {
243247
return actualMediaSource?.createPeriod(id, allocator, startPositionUs)
244-
?: throw IllegalStateException("Media source not prepared")
248+
?: throw IOException("Media source not prepared")
245249
}
246250

247251
override fun releasePeriod(mediaPeriod: MediaPeriod) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,20 @@ class PlaybackService : MediaLibraryService() {
560560
)
561561
}
562562
ToastManager.show("Playback error")
563+
564+
// Remove failed item and try to play next
565+
val currentIndex = player.currentMediaItemIndex
566+
val hasNext = player.hasNextMediaItem()
567+
568+
if (currentIndex >= 0 && currentIndex < player.mediaItemCount) {
569+
player.removeMediaItem(currentIndex)
570+
571+
if (hasNext && player.mediaItemCount > 0) {
572+
// If there was a next item, it's now at the current index
573+
player.prepare()
574+
player.play()
575+
}
576+
}
563577
}
564578
}
565579
}

0 commit comments

Comments
 (0)