Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

ListenersHolder collaborator #30

Merged
merged 8 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/src/main/java/com/novoda/noplayer/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import java.util.Map;

public interface Player extends PlayerState, PlayerListeners {
public interface Player extends PlayerState {

void play();

Expand Down Expand Up @@ -35,6 +35,8 @@ public interface Player extends PlayerState, PlayerListeners {

List<PlayerAudioTrack> getAudioTracks();

PlayerListenersHolder getListenersHolder();

interface PreReleaseListener {

PreReleaseListener NULL_IMPL = new PreReleaseListener() {
Expand All @@ -52,7 +54,6 @@ interface PlayerError {
String getType();

Throwable getCause();

}

interface ErrorListener {
Expand Down
42 changes: 0 additions & 42 deletions core/src/main/java/com/novoda/noplayer/PlayerListeners.java

This file was deleted.

41 changes: 11 additions & 30 deletions core/src/main/java/com/novoda/noplayer/PlayerListenersHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.novoda.noplayer.listeners.StateChangedListeners;
import com.novoda.noplayer.listeners.VideoSizeChangedListeners;

public class PlayerListenersHolder implements PlayerListeners {
public class PlayerListenersHolder {

private final ErrorListeners errorListeners;
private final PreparedListeners preparedListeners;
Expand All @@ -37,138 +37,119 @@ public PlayerListenersHolder() {
bitrateChangedListeners = new BitrateChangedListeners();
}

@Override
public void addErrorListener(Player.ErrorListener errorListener) {
errorListeners.add(errorListener);
}

@Override
public void removeErrorListener(Player.ErrorListener errorListener) {
errorListeners.remove(errorListener);
}

@Override
public void addPreparedListener(Player.PreparedListener preparedListener) {
preparedListeners.add(preparedListener);
}

@Override
public void removePreparedListener(Player.PreparedListener preparedListener) {
preparedListeners.remove(preparedListener);
}

@Override
public void addBufferStateListener(Player.BufferStateListener bufferStateListener) {
bufferStateListeners.add(bufferStateListener);
}

@Override
public void removeBufferStateListener(Player.BufferStateListener bufferStateListener) {
bufferStateListeners.remove(bufferStateListener);
}

@Override
public void addCompletionListener(Player.CompletionListener completionListener) {
completionListeners.add(completionListener);
}

@Override
public void removeCompletionListener(Player.CompletionListener completionListener) {
completionListeners.remove(completionListener);
}

@Override
public void addStateChangedListener(Player.StateChangedListener stateChangedListener) {
stateChangedListeners.add(stateChangedListener);
}

@Override
public void removeStateChangedListener(Player.StateChangedListener stateChangedListener) {
stateChangedListeners.remove(stateChangedListener);
}

@Override
public void addInfoListener(Player.InfoListener infoListener) {
infoListeners.add(infoListener);
}

@Override
public void removeInfoListener(Player.InfoListener infoListener) {
infoListeners.remove(infoListener);
}

@Override
public void addBitrateChangedListener(BitrateChangedListener bitrateChangedListener) {
bitrateChangedListeners.add(bitrateChangedListener);
}

@Override
public void removeBitrateChangedListener(BitrateChangedListener bitrateChangedListener) {
bitrateChangedListeners.remove(bitrateChangedListener);
}

@Override
public void setPreReleaseListener(Player.PreReleaseListener playerReleaseListener) {
this.playerReleaseListener = playerReleaseListener;
}

@Override
public void addHeartbeatCallback(Heart.Heartbeat.Callback<Player> callback) {
heartbeatCallbacks.registerCallback(callback);
}

@Override
public void removeHeartbeatCallback(Heart.Heartbeat.Callback<Player> callback) {
heartbeatCallbacks.unregisterCallback(callback);
}

@Override
public void addVideoSizeChangedListener(Player.VideoSizeChangedListener videoSizeChangedListener) {
videoSizeChangedListeners.add(videoSizeChangedListener);
}

@Override
public void removeVideoSizeChangedListener(Player.VideoSizeChangedListener videoSizeChangedListener) {
videoSizeChangedListeners.remove(videoSizeChangedListener);
}

protected final ErrorListeners getErrorListeners() {
public ErrorListeners getErrorListeners() {
return errorListeners;
}

protected final PreparedListeners getPreparedListeners() {
public PreparedListeners getPreparedListeners() {
return preparedListeners;
}

protected final BufferStateListeners getBufferStateListeners() {
public BufferStateListeners getBufferStateListeners() {
return bufferStateListeners;
}

protected final CompletionListeners getCompletionListeners() {
public CompletionListeners getCompletionListeners() {
return completionListeners;
}

protected final StateChangedListeners getStateChangedListeners() {
public StateChangedListeners getStateChangedListeners() {
return stateChangedListeners;
}

protected final InfoListeners getInfoListeners() {
public InfoListeners getInfoListeners() {
return infoListeners;
}

protected final Player.PreReleaseListener getPlayerReleaseListener() {
public Player.PreReleaseListener getPlayerReleaseListener() {
return playerReleaseListener;
}

protected final HeartbeatCallbacks<Player> getHeartbeatCallbacks() {
public HeartbeatCallbacks<Player> getHeartbeatCallbacks() {
return heartbeatCallbacks;
}

protected final VideoSizeChangedListeners getVideoSizeChangedListeners() {
public VideoSizeChangedListeners getVideoSizeChangedListeners() {
return videoSizeChangedListeners;
}

protected final BitrateChangedListeners getBitrateChangedListeners() {
public BitrateChangedListeners getBitrateChangedListeners() {
return bitrateChangedListeners;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@

import java.util.List;

public class ExoPlayerTwoImpl extends PlayerListenersHolder implements Player {
public class ExoPlayerTwoImpl implements Player {

private static final boolean RESET_POSITION = true;
private static final boolean DO_NOT_RESET_STATE = false;

private final SimpleExoPlayer exoPlayer;
private final PlayerListenersHolder listenersHolder;
private final MediaSourceFactory mediaSourceFactory;
private final ExoPlayerForwarder forwarder;
private final ExoPlayerAudioTrackSelector trackSelector;
Expand All @@ -64,10 +65,11 @@ public static ExoPlayerTwoImpl newInstance(Context context) {
SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(context), trackSelector, new DefaultLoadControl());
LoadTimeout loadTimeout = new LoadTimeout(new SystemClock(), new Handler(Looper.getMainLooper()));
VideoContainer videoContainer = VideoContainer.empty();
PlayerListenersHolder listenersHolder = new PlayerListenersHolder();

return new ExoPlayerTwoImpl(
exoPlayer,
mediaSourceFactory,
listenersHolder, mediaSourceFactory,
new ExoPlayerForwarder(),
loadTimeout,
exoPlayerAudioTrackSelector,
Expand All @@ -77,41 +79,43 @@ public static ExoPlayerTwoImpl newInstance(Context context) {
}

ExoPlayerTwoImpl(SimpleExoPlayer exoPlayer,
PlayerListenersHolder listenersHolder,
MediaSourceFactory mediaSourceFactory,
ExoPlayerForwarder exoPlayerForwarder,
LoadTimeout loadTimeoutParam,
ExoPlayerAudioTrackSelector trackSelector,
Heart heart,
VideoContainer videoContainer) {
this.exoPlayer = exoPlayer;
this.listenersHolder = listenersHolder;
this.mediaSourceFactory = mediaSourceFactory;
this.loadTimeout = loadTimeoutParam;
this.forwarder = exoPlayerForwarder;
this.trackSelector = trackSelector;
this.heart = heart;
this.videoContainer = videoContainer;

heart.bind(new Heart.Heartbeat<>(getHeartbeatCallbacks(), this));
forwarder.bind(getPreparedListeners(), this);
forwarder.bind(getCompletionListeners());
forwarder.bind(getErrorListeners(), this);
forwarder.bind(getBufferStateListeners());
forwarder.bind(getVideoSizeChangedListeners());
forwarder.bind(getBitrateChangedListeners());
forwarder.bind(getInfoListeners());
addPreparedListener(new PreparedListener() {
heart.bind(new Heart.Heartbeat<>(listenersHolder.getHeartbeatCallbacks(), this));
forwarder.bind(listenersHolder.getPreparedListeners(), this);
forwarder.bind(listenersHolder.getCompletionListeners());
forwarder.bind(listenersHolder.getErrorListeners(), this);
forwarder.bind(listenersHolder.getBufferStateListeners());
forwarder.bind(listenersHolder.getVideoSizeChangedListeners());
forwarder.bind(listenersHolder.getBitrateChangedListeners());
forwarder.bind(listenersHolder.getInfoListeners());
listenersHolder.addPreparedListener(new PreparedListener() {
@Override
public void onPrepared(PlayerState playerState) {
loadTimeout.cancel();
}
});
addErrorListener(new ErrorListener() {
listenersHolder.addErrorListener(new ErrorListener() {
@Override
public void onError(Player player, PlayerError error) {
loadTimeout.cancel();
}
});
addVideoSizeChangedListener(new VideoSizeChangedListener() {
listenersHolder.addVideoSizeChangedListener(new VideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
videoWidth = width;
Expand Down Expand Up @@ -155,7 +159,7 @@ public void play() {
showContainer();
heart.startBeatingHeart();
exoPlayer.setPlayWhenReady(true);
getStateChangedListeners().onVideoPlaying();
listenersHolder.getStateChangedListeners().onVideoPlaying();
}

@Override
Expand All @@ -167,7 +171,7 @@ public void play(VideoPosition position) {
@Override
public void pause() {
exoPlayer.setPlayWhenReady(false);
getStateChangedListeners().onVideoPaused();
listenersHolder.getStateChangedListeners().onVideoPaused();
if (heart.isBeating()) {
heart.stopBeatingHeart();
heart.forceBeat();
Expand All @@ -191,8 +195,8 @@ public void stop() {

@Override
public void release() {
getPlayerReleaseListener().onPlayerPreRelease(this);
getStateChangedListeners().onVideoReleased();
listenersHolder.getPlayerReleaseListener().onPlayerPreRelease(this);
listenersHolder.getStateChangedListeners().onVideoReleased();
loadTimeout.cancel();
heart.stopBeatingHeart();
exoPlayer.release();
Expand All @@ -201,7 +205,7 @@ public void release() {

@Override
public void loadVideo(Uri uri, ContentType contentType) {
getPreparedListeners().reset();
listenersHolder.getPreparedListeners().reset();
showContainer();
exoPlayer.addListener(forwarder.exoPlayerEventListener());
exoPlayer.setVideoDebugListener(forwarder.videoRendererEventListener());
Expand Down Expand Up @@ -242,6 +246,11 @@ public List<PlayerAudioTrack> getAudioTracks() {
return trackSelector.getAudioTracks();
}

@Override
public PlayerListenersHolder getListenersHolder() {
return listenersHolder;
}

private void showContainer() {
videoContainer.show();
}
Expand Down
Loading