-Hooking playback manager and play queue into main video player.
This commit is contained in:
parent
0f6296c9b8
commit
20f8c3c262
9 changed files with 147 additions and 44 deletions
|
|
@ -72,6 +72,7 @@ import org.schabi.newpipe.Downloader;
|
|||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.extractor.stream_info.StreamInfo;
|
||||
import org.schabi.newpipe.playlist.PlayQueue;
|
||||
import org.schabi.newpipe.util.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.DecimalFormat;
|
||||
|
|
@ -257,7 +258,6 @@ public abstract class BasePlayer implements Player.EventListener,
|
|||
changeState(STATE_LOADING);
|
||||
|
||||
isPrepared = false;
|
||||
mediaSource = buildMediaSource(url, format);
|
||||
|
||||
if (simpleExoPlayer.getPlaybackState() != Player.STATE_IDLE) simpleExoPlayer.stop();
|
||||
if (videoStartPos > 0) simpleExoPlayer.seekTo(videoStartPos);
|
||||
|
|
@ -548,7 +548,7 @@ public abstract class BasePlayer implements Player.EventListener,
|
|||
@Override
|
||||
public void onPositionDiscontinuity() {
|
||||
int newIndex = simpleExoPlayer.getCurrentWindowIndex();
|
||||
|
||||
playbackManager.refreshMedia(newIndex);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
|
|
@ -567,12 +567,12 @@ public abstract class BasePlayer implements Player.EventListener,
|
|||
|
||||
@Override
|
||||
public void sync(final StreamInfo info) {
|
||||
|
||||
videoTitle = info.title;
|
||||
channelName = info.uploader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MediaSource sourceOf(final StreamInfo info) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -454,9 +454,15 @@ public class MainVideoPlayer extends Activity {
|
|||
@Override
|
||||
public boolean onDoubleTap(MotionEvent e) {
|
||||
if (DEBUG) Log.d(TAG, "onDoubleTap() called with: e = [" + e + "]" + "rawXy = " + e.getRawX() + ", " + e.getRawY() + ", xy = " + e.getX() + ", " + e.getY());
|
||||
if (!playerImpl.isPlaying()) return false;
|
||||
if (e.getX() > playerImpl.getRootView().getWidth() / 2) playerImpl.onFastForward();
|
||||
else playerImpl.onFastRewind();
|
||||
//if (!playerImpl.isPlaying()) return false;
|
||||
|
||||
if (e.getX() > playerImpl.getRootView().getWidth() / 2)
|
||||
playerImpl.playQueue.setIndex(playerImpl.playQueue.getIndex() + 1);
|
||||
//playerImpl.onFastForward();
|
||||
else
|
||||
playerImpl.playQueue.setIndex(playerImpl.playQueue.getIndex() - 1);
|
||||
//playerImpl.onFastRewind();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,10 +15,14 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.annotations.NonNull;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class PlaybackManager {
|
||||
|
||||
private static final int WINDOW_SIZE = 5;
|
||||
|
||||
private DynamicConcatenatingMediaSource mediaSource;
|
||||
private List<PlayQueueItem> queueSource;
|
||||
private int sourceIndex;
|
||||
|
|
@ -58,8 +62,11 @@ public class PlaybackManager {
|
|||
load(0);
|
||||
}
|
||||
|
||||
public void changeSource(final int index) {
|
||||
|
||||
public void changeSource(final MediaSource newSource) {
|
||||
listener.block();
|
||||
this.mediaSource.removeMediaSource(0);
|
||||
this.mediaSource.addMediaSource(0, newSource);
|
||||
listener.unblock();
|
||||
}
|
||||
|
||||
public void refreshMedia(final int newMediaIndex) {
|
||||
|
|
@ -71,7 +78,7 @@ public class PlaybackManager {
|
|||
queueSource.remove(0);
|
||||
} else {
|
||||
//something went wrong
|
||||
init();
|
||||
reload();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +92,8 @@ public class PlaybackManager {
|
|||
private Subscription loaderReactor;
|
||||
|
||||
private void load() {
|
||||
if (mediaSource.getSize() < 5 && queueSource.size() < 5) load(mediaSource.getSize());
|
||||
if (mediaSource.getSize() < WINDOW_SIZE && queueSource.size() < WINDOW_SIZE)
|
||||
load(mediaSource.getSize());
|
||||
}
|
||||
|
||||
private void load(final int from) {
|
||||
|
|
@ -94,23 +102,33 @@ public class PlaybackManager {
|
|||
if (loaderReactor != null) loaderReactor.cancel();
|
||||
|
||||
List<Maybe<StreamInfo>> maybes = new ArrayList<>();
|
||||
for (int i = from; i < 5; i++) {
|
||||
for (int i = from; i < WINDOW_SIZE; i++) {
|
||||
final int index = playQueue.getIndex() + i;
|
||||
final PlayQueueItem item = playQueue.get(index);
|
||||
queueSource.set(i, item);
|
||||
|
||||
if (queueSource.size() > i) queueSource.set(i, item);
|
||||
else queueSource.add(item);
|
||||
|
||||
maybes.add(item.getStream());
|
||||
}
|
||||
|
||||
Maybe.concat(maybes).subscribe(new Subscriber<StreamInfo>() {
|
||||
Maybe.concat(maybes).subscribe(getSubscriber());
|
||||
}
|
||||
|
||||
private Subscriber<StreamInfo> getSubscriber() {
|
||||
return new Subscriber<StreamInfo>() {
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
if (loaderReactor != null) loaderReactor.cancel();
|
||||
loaderReactor = s;
|
||||
s.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(StreamInfo streamInfo) {
|
||||
mediaSource.addMediaSource(listener.sourceOf(streamInfo));
|
||||
onLoaded();
|
||||
tryUnblock();
|
||||
loaderReactor.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -120,11 +138,13 @@ public class PlaybackManager {
|
|||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
if (loaderReactor != null) loaderReactor.cancel();
|
||||
loaderReactor = null;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
private void onLoaded() {
|
||||
private void tryUnblock() {
|
||||
if (mediaSource.getSize() > 0 && queueSource.size() > 0) listener.unblock();
|
||||
}
|
||||
|
||||
|
|
@ -134,11 +154,15 @@ public class PlaybackManager {
|
|||
}
|
||||
|
||||
private void clear(int from) {
|
||||
listener.block();
|
||||
while (mediaSource.getSize() > from) {
|
||||
queueSource.remove(from);
|
||||
mediaSource.removeMediaSource(from);
|
||||
}
|
||||
}
|
||||
|
||||
private void clear() {
|
||||
listener.block();
|
||||
clear(0);
|
||||
listener.unblock();
|
||||
}
|
||||
|
||||
|
|
@ -153,7 +177,7 @@ public class PlaybackManager {
|
|||
|
||||
@Override
|
||||
public void onNext(@NonNull PlayQueueEvent event) {
|
||||
if (playQueue.getStreams().size() - playQueue.getIndex() < 10 && !playQueue.isComplete()) {
|
||||
if (playQueue.getStreams().size() - playQueue.getIndex() < WINDOW_SIZE && !playQueue.isComplete()) {
|
||||
listener.block();
|
||||
playQueue.fetch();
|
||||
}
|
||||
|
|
@ -177,14 +201,14 @@ public class PlaybackManager {
|
|||
load(1);
|
||||
break;
|
||||
case CLEAR:
|
||||
clear(0);
|
||||
clear();
|
||||
break;
|
||||
case NEXT:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
onLoaded();
|
||||
tryUnblock();
|
||||
if (playQueueReactor != null) playQueueReactor.request(1);
|
||||
}
|
||||
|
||||
|
|
@ -195,12 +219,13 @@ public class PlaybackManager {
|
|||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
// Never completes, only canceled
|
||||
dispose();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
if (playQueueReactor != null) playQueueReactor.cancel();
|
||||
playQueueReactor = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,11 @@ import org.schabi.newpipe.R;
|
|||
import org.schabi.newpipe.extractor.MediaFormat;
|
||||
import org.schabi.newpipe.extractor.stream.AudioStream;
|
||||
import org.schabi.newpipe.extractor.stream.VideoStream;
|
||||
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
||||
import org.schabi.newpipe.extractor.playlist.PlayListInfo;
|
||||
import org.schabi.newpipe.playlist.ExternalPlayQueue;
|
||||
import org.schabi.newpipe.util.AnimationUtils;
|
||||
import org.schabi.newpipe.util.Utils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -198,7 +202,7 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
|
|||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void handleIntent(Intent intent) {
|
||||
public void handleIntent2(Intent intent) {
|
||||
super.handleIntent(intent);
|
||||
if (DEBUG) Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]");
|
||||
if (intent == null) return;
|
||||
|
|
@ -217,6 +221,38 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
|
|||
play(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MediaSource sourceOf(final StreamInfo info) {
|
||||
videoStreamsList = Utils.getSortedStreamVideosList(context, info.video_streams, info.video_only_streams, false);
|
||||
videoOnlyAudioStream = Utils.getHighestQualityAudio(info.audio_streams);
|
||||
|
||||
return buildMediaSource(getSelectedVideoStream().url, MediaFormat.getSuffixById(getSelectedVideoStream().format));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unblock() {
|
||||
play(true);
|
||||
super.unblock();
|
||||
}
|
||||
|
||||
public void handleIntent(Intent intent) {
|
||||
if (intent == null) return;
|
||||
|
||||
selectedIndexStream = 0;
|
||||
|
||||
String url = intent.getStringExtra("url");
|
||||
int nextPage = intent.getIntExtra("nextPage", 0);
|
||||
int index = intent.getIntExtra("index", 0);
|
||||
|
||||
PlayListInfo info;
|
||||
Serializable serializable = intent.getSerializableExtra("stream");
|
||||
if (serializable instanceof PlayListInfo) info = (PlayListInfo) serializable;
|
||||
else return;
|
||||
|
||||
playQueue = new ExternalPlayQueue(url, info, nextPage, index);
|
||||
playbackManager = new PlaybackManager(this, playQueue);
|
||||
mediaSource = playbackManager.getMediaSource();
|
||||
}
|
||||
|
||||
public void play(boolean autoPlay) {
|
||||
playUrl(getSelectedVideoStream().url, MediaFormat.getSuffixById(getSelectedVideoStream().format), autoPlay);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue