1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-04-06 02:37:18 +00:00

Properly handle item errors during media browser loading

Non-item errors, i.e. critical parsing errors of the page, are still handled properly.
This commit is contained in:
Stypox 2025-02-16 10:45:02 +01:00
parent dc62d211f5
commit e5458bcb14
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 19 additions and 37 deletions

View File

@ -13,7 +13,6 @@ import androidx.media.MediaBrowserServiceCompat.Result
import androidx.media.utils.MediaConstants
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.core.SingleSource
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.schedulers.Schedulers
import org.schabi.newpipe.MainActivity.DEBUG
@ -25,10 +24,8 @@ import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity
import org.schabi.newpipe.extractor.InfoItem
import org.schabi.newpipe.extractor.InfoItem.InfoType
import org.schabi.newpipe.extractor.ListInfo
import org.schabi.newpipe.extractor.channel.ChannelInfoItem
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
import org.schabi.newpipe.extractor.search.SearchInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem
@ -86,6 +83,10 @@ class MediaBrowserImpl(
//region onLoadChildren
fun onLoadChildren(parentId: String, result: Result<List<MediaBrowserCompat.MediaItem>>) {
if (DEBUG) {
Log.d(TAG, "onLoadChildren($parentId)")
}
result.detach() // allows sendResult() to happen later
disposables.add(
onLoadChildren(parentId)
@ -101,10 +102,6 @@ class MediaBrowserImpl(
}
private fun onLoadChildren(parentId: String): Single<List<MediaBrowserCompat.MediaItem>> {
if (DEBUG) {
Log.d(TAG, "onLoadChildren($parentId)")
}
try {
val parentIdUri = Uri.parse(parentId)
val path = ArrayList(parentIdUri.pathSegments)
@ -353,15 +350,12 @@ class MediaBrowserImpl(
private fun populateRemotePlaylist(playlistId: Long): Single<List<MediaBrowserCompat.MediaItem>> {
return RemotePlaylistManager(database).getPlaylist(playlistId).firstOrError()
.flatMap { ExtractorHelper.getPlaylistInfo(it.serviceId, it.url, false) }
.flatMap { info ->
info.errors.firstOrNull { it !is ContentNotSupportedException }?.let {
return@flatMap Single.error(it)
.map {
// ignore it.errors, i.e. ignore errors about specific items, since there would
// be no way to show the error properly in Android Auto anyway
it.relatedItems.mapIndexed { index, item ->
createRemotePlaylistStreamMediaItem(playlistId, item, index)
}
Single.just(
info.relatedItems.mapIndexed { index, item ->
createRemotePlaylistStreamMediaItem(playlistId, item, index)
}
)
}
}
//endregion
@ -371,10 +365,16 @@ class MediaBrowserImpl(
query: String,
result: Result<List<MediaBrowserCompat.MediaItem>>
) {
if (DEBUG) {
Log.d(TAG, "onSearch($query)")
}
result.detach() // allows sendResult() to happen later
disposables.add(
searchMusicBySongTitle(query)
.flatMap { this.mediaItemsFromInfoItemList(it) }
// ignore it.errors, i.e. ignore errors about specific items, since there would
// be no way to show the error properly in Android Auto anyway
.map { it.relatedItems.mapNotNull(this::createInfoItemMediaItem) }
.subscribeOn(Schedulers.io())
.subscribe(
{ result.sendResult(it) },
@ -391,20 +391,6 @@ class MediaBrowserImpl(
val serviceId = ServiceHelper.getSelectedServiceId(context)
return ExtractorHelper.searchFor(serviceId, query, listOf(), "")
}
private fun mediaItemsFromInfoItemList(
result: ListInfo<InfoItem>
): SingleSource<List<MediaBrowserCompat.MediaItem>> {
result.errors.firstOrNull()?.let { return@mediaItemsFromInfoItemList Single.error(it) }
return try {
Single.just(
result.relatedItems.mapNotNull { item -> this.createInfoItemMediaItem(item) }
)
} catch (e: Exception) {
Single.error(e)
}
}
//endregion
companion object {

View File

@ -17,7 +17,6 @@ import org.schabi.newpipe.NewPipeDatabase
import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.InfoItem.InfoType
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler
import org.schabi.newpipe.local.playlist.LocalPlaylistManager
import org.schabi.newpipe.local.playlist.RemotePlaylistManager
@ -131,12 +130,9 @@ class MediaBrowserPlaybackPreparer(
private fun extractRemotePlayQueue(playlistId: Long, index: Int): Single<PlayQueue> {
return RemotePlaylistManager(database).getPlaylist(playlistId).firstOrError()
.flatMap { ExtractorHelper.getPlaylistInfo(it.serviceId, it.url, false) }
.flatMap { info ->
info.errors.firstOrNull { it !is ContentNotSupportedException }?.let {
return@flatMap Single.error(it)
}
Single.just(PlaylistPlayQueue(info, index))
}
// ignore info.errors, i.e. ignore errors about specific items, since there would
// be no way to show the error properly in Android Auto anyway
.map { info -> PlaylistPlayQueue(info, index) }
}
private fun extractPlayQueueFromMediaId(mediaId: String): Single<PlayQueue> {