Merge pull request #7036 from Douile/fix/queue-metadata

Load full stream info when enqueuing a stream
This commit is contained in:
Stypox 2022-01-07 21:57:30 +01:00 committed by GitHub
commit 7907182e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package org.schabi.newpipe.util;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -21,6 +22,7 @@ import org.schabi.newpipe.util.external_communication.ShareUtils;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.schedulers.Schedulers;
@ -63,20 +65,24 @@ public enum StreamDialogEntry {
* Info: Add this entry within showStreamDialog. * Info: Add this entry within showStreamDialog.
*/ */
enqueue(R.string.enqueue_stream, (fragment, item) -> { enqueue(R.string.enqueue_stream, (fragment, item) -> {
NavigationHelper.enqueueOnPlayer(fragment.getContext(), new SinglePlayQueue(item)); fetchItemInfoIfSparse(fragment, item, fullItem ->
NavigationHelper.enqueueOnPlayer(fragment.getContext(), fullItem));
}), }),
enqueue_next(R.string.enqueue_next_stream, (fragment, item) -> { enqueue_next(R.string.enqueue_next_stream, (fragment, item) -> {
NavigationHelper.enqueueNextOnPlayer(fragment.getContext(), new SinglePlayQueue(item)); fetchItemInfoIfSparse(fragment, item, fullItem ->
NavigationHelper.enqueueNextOnPlayer(fragment.getContext(), fullItem));
}), }),
start_here_on_background(R.string.start_here_on_background, (fragment, item) -> start_here_on_background(R.string.start_here_on_background, (fragment, item) -> {
NavigationHelper.playOnBackgroundPlayer(fragment.getContext(), fetchItemInfoIfSparse(fragment, item, fullItem ->
new SinglePlayQueue(item), true)), NavigationHelper.playOnBackgroundPlayer(fragment.getContext(), fullItem, true));
}),
start_here_on_popup(R.string.start_here_on_popup, (fragment, item) -> start_here_on_popup(R.string.start_here_on_popup, (fragment, item) -> {
NavigationHelper.playOnPopupPlayer(fragment.getContext(), fetchItemInfoIfSparse(fragment, item, fullItem ->
new SinglePlayQueue(item), true)), NavigationHelper.playOnPopupPlayer(fragment.getContext(), fullItem, true));
}),
set_as_playlist_thumbnail(R.string.set_as_playlist_thumbnail, (fragment, item) -> { set_as_playlist_thumbnail(R.string.set_as_playlist_thumbnail, (fragment, item) -> {
}), // has to be set manually }), // has to be set manually
@ -218,4 +224,39 @@ public enum StreamDialogEntry {
fragment.requireActivity().getSupportFragmentManager(), fragment.requireActivity().getSupportFragmentManager(),
item.getServiceId(), uploaderUrl, item.getUploaderName()); item.getServiceId(), uploaderUrl, item.getUploaderName());
} }
/////////////////////////////////////////////
// helper functions //
/////////////////////////////////////////////
private static void fetchItemInfoIfSparse(final Fragment fragment,
final StreamInfoItem item,
final Consumer<SinglePlayQueue> callback) {
if (!(item.getStreamType() == StreamType.LIVE_STREAM
|| item.getStreamType() == StreamType.AUDIO_LIVE_STREAM)
&& item.getDuration() < 0) {
// Sparse item: fetched by fast fetch
ExtractorHelper.getStreamInfo(
item.getServiceId(),
item.getUrl(),
false
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
final HistoryRecordManager recordManager =
new HistoryRecordManager(fragment.getContext());
recordManager.saveStreamState(result, 0)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> Log.e("StreamDialogEntry",
throwable.toString()))
.subscribe();
callback.accept(new SinglePlayQueue(result));
}, throwable -> Log.e("StreamDialogEntry", throwable.toString()));
} else {
callback.accept(new SinglePlayQueue(item));
}
}
} }