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

Commit

Permalink
Return boolean for functions selecting tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
zegnus committed Jun 19, 2017
1 parent 39013f9 commit 6794007
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 24 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/com/novoda/noplayer/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public interface Player extends PlayerState {

void detach(PlayerView playerView);

void selectAudioTrack(PlayerAudioTrack audioTrack);
boolean selectAudioTrack(PlayerAudioTrack audioTrack);

void showSubtitleTrack(PlayerSubtitleTrack subtitleTrack);
boolean showSubtitleTrack(PlayerSubtitleTrack subtitleTrack);

void hideSubtitleTrack();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ void loadVideo(Uri uri, ContentType contentType, ExoPlayerForwarder forwarder) {
exoPlayer.prepare(mediaSource, RESET_POSITION, DO_NOT_RESET_STATE);
}

void selectAudioTrack(PlayerAudioTrack audioTrack) {
boolean selectAudioTrack(PlayerAudioTrack audioTrack) {
assertVideoLoaded();
audioTrackSelector.selectAudioTrack(audioTrack, rendererTypeRequester);
return audioTrackSelector.selectAudioTrack(audioTrack, rendererTypeRequester);
}

List<PlayerAudioTrack> getAudioTracks() {
Expand All @@ -133,9 +133,9 @@ void removeSubtitleRendererOutput() {
exoPlayer.setTextOutput(null);
}

void selectSubtitleTrack(PlayerSubtitleTrack subtitleTrack) {
boolean selectSubtitleTrack(PlayerSubtitleTrack subtitleTrack) {
assertVideoLoaded();
subtitleTrackSelector.selectTextTrack(subtitleTrack, rendererTypeRequester);
return subtitleTrackSelector.selectTextTrack(subtitleTrack, rendererTypeRequester);
}

List<PlayerSubtitleTrack> getSubtitleTracks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,23 @@ public void detach(PlayerView playerView) {
this.playerView = null;
}

/**
* @return Whether the selection was successful
*/
@Override
public void selectAudioTrack(PlayerAudioTrack audioTrack) {
exoPlayer.selectAudioTrack(audioTrack);
public boolean selectAudioTrack(PlayerAudioTrack audioTrack) {
return exoPlayer.selectAudioTrack(audioTrack);
}

/**
*
* @return Whether the selection was successful
*/
@Override
public void showSubtitleTrack(PlayerSubtitleTrack subtitleTrack) {
public boolean showSubtitleTrack(PlayerSubtitleTrack subtitleTrack) {
setSubtitleRendererOutput();
exoPlayer.selectSubtitleTrack(subtitleTrack);
playerView.showSubtitles();
return exoPlayer.selectSubtitleTrack(subtitleTrack);
}

private void setSubtitleRendererOutput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public ExoPlayerAudioTrackSelector(ExoPlayerTrackSelector trackSelector, TrackSe
this.trackSelectionFactory = trackSelectionFactory;
}

public void selectAudioTrack(PlayerAudioTrack audioTrack, RendererTypeRequester rendererTypeRequester) {
public boolean selectAudioTrack(PlayerAudioTrack audioTrack, RendererTypeRequester rendererTypeRequester) {
TrackGroupArray trackGroups = trackSelector.trackGroups(AUDIO, rendererTypeRequester);

MappingTrackSelector.SelectionOverride selectionOverride = new MappingTrackSelector.SelectionOverride(
trackSelectionFactory,
audioTrack.groupIndex(),
audioTrack.formatIndex()
);
trackSelector.setSelectionOverride(AUDIO, rendererTypeRequester, trackGroups, selectionOverride);
return trackSelector.setSelectionOverride(AUDIO, rendererTypeRequester, trackGroups, selectionOverride);
}

public List<PlayerAudioTrack> getAudioTracks(RendererTypeRequester rendererTypeRequester) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public ExoPlayerSubtitleTrackSelector(ExoPlayerTrackSelector trackSelector, Trac
this.trackSelectionFactory = trackSelectionFactory;
}

public void selectTextTrack(PlayerSubtitleTrack subtitleTrack, RendererTypeRequester rendererTypeRequester) {
public boolean selectTextTrack(PlayerSubtitleTrack subtitleTrack, RendererTypeRequester rendererTypeRequester) {
TrackGroupArray trackGroups = trackSelector.trackGroups(TEXT, rendererTypeRequester);

MappingTrackSelector.SelectionOverride selectionOverride = new MappingTrackSelector.SelectionOverride(
trackSelectionFactory,
subtitleTrack.groupIndex(),
subtitleTrack.formatIndex()
);
trackSelector.setSelectionOverride(TEXT, rendererTypeRequester, trackGroups, selectionOverride);
return trackSelector.setSelectionOverride(TEXT, rendererTypeRequester, trackGroups, selectionOverride);
}

public List<PlayerSubtitleTrack> getSubtitleTracks(RendererTypeRequester rendererTypeRequester) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ private int mappedTrackInfoLength() {
return trackSelector.getCurrentMappedTrackInfo().length;
}

void setSelectionOverride(TrackType trackType,
RendererTypeRequester rendererTypeRequester,
TrackGroupArray trackGroups,
MappingTrackSelector.SelectionOverride selectionOverride) {
boolean setSelectionOverride(TrackType trackType,
RendererTypeRequester rendererTypeRequester,
TrackGroupArray trackGroups,
MappingTrackSelector.SelectionOverride selectionOverride) {
Optional<Integer> audioRendererIndex = rendererTrackIndexExtractor.extract(trackType, mappedTrackInfoLength(), rendererTypeRequester);
if (audioRendererIndex.isPresent()) {
trackSelector.setSelectionOverride(audioRendererIndex.get(), trackGroups, selectionOverride);
return true;
} else {
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ List<PlayerAudioTrack> getAudioTracks(MediaPlayer mediaPlayer) {
return audioTracks;
}

void selectAudioTrack(MediaPlayer mediaPlayer, PlayerAudioTrack playerAudioTrack) {
boolean selectAudioTrack(MediaPlayer mediaPlayer, PlayerAudioTrack playerAudioTrack) {
if (mediaPlayer == null) {
throw new NullPointerException("You can only call selectAudioTrack() when video is prepared.");
}

mediaPlayer.selectTrack(playerAudioTrack.groupIndex());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ List<PlayerAudioTrack> getAudioTracks() {
return trackSelector.getAudioTracks(mediaPlayer);
}

void selectAudioTrack(PlayerAudioTrack playerAudioTrack) {
boolean selectAudioTrack(PlayerAudioTrack playerAudioTrack) {
assertIsInPlaybackState();
trackSelector.selectAudioTrack(mediaPlayer, playerAudioTrack);
return trackSelector.selectAudioTrack(mediaPlayer, playerAudioTrack);
}

void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener seekToResettingSeekListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,22 @@ public void detach(PlayerView playerView) {
buggyVideoDriverPreventer.clear(playerView.getContainerView());
}

/**
* @return Whether the selection was successful
*/
@Override
public void selectAudioTrack(PlayerAudioTrack audioTrack) {
mediaPlayer.selectAudioTrack(audioTrack);
public boolean selectAudioTrack(PlayerAudioTrack audioTrack) {
return mediaPlayer.selectAudioTrack(audioTrack);
}

/**
* @return Whether the selection was successful
*/
@Override
public void showSubtitleTrack(PlayerSubtitleTrack subtitleTrack) {
public boolean showSubtitleTrack(PlayerSubtitleTrack subtitleTrack) {
SubtitlesError subtitlesError = new SubtitlesError("Subtitles not implemented for Android Media Player", new IllegalStateException());
listenersHolder.getErrorListeners().onError(this, subtitlesError);
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.android.exoplayer2.source.MediaSource;
import com.novoda.noplayer.ContentType;
import com.novoda.noplayer.PlayerAudioTrack;
import com.novoda.noplayer.PlayerSubtitleTrack;
import com.novoda.noplayer.VideoDuration;
import com.novoda.noplayer.VideoPosition;
import com.novoda.noplayer.exoplayer.forwarder.ExoPlayerForwarder;
Expand Down Expand Up @@ -164,6 +165,15 @@ public void whenGettingAudioTracks_thenThrowsIllegalStateException() {

facade.getAudioTracks();
}

@Test
public void selectSubtitleTrack_thenThrowsIllegalStateException() {
thrown.expect(ExceptionMatcher.matches("Video must be loaded before trying to interact with the player", IllegalStateException.class));

PlayerSubtitleTrack subtitleTrack = mock(PlayerSubtitleTrack.class);

facade.selectSubtitleTrack(subtitleTrack);
}
}

public static class GivenVideoIsLoaded extends Base {
Expand Down Expand Up @@ -293,6 +303,55 @@ public void whenSelectingAudioTrack_thenDelegatesToTrackSelector() {
verify(audioTrackSelector).selectAudioTrack(audioTrack, rendererTypeRequester);
}

@Test
public void givenSelectingAudioTrackSuceeds_whenSelectingAudioTrack_thenReturnsTrue() {
PlayerAudioTrack audioTrack = mock(PlayerAudioTrack.class);
given(audioTrackSelector.selectAudioTrack(audioTrack, rendererTypeRequester)).willReturn(true);

boolean success = facade.selectAudioTrack(audioTrack);

assertThat(success).isTrue();
}

@Test
public void givenSelectingAudioTrackFails_whenSelectingAudioTrack_thenReturnsFalse() {
PlayerAudioTrack audioTrack = mock(PlayerAudioTrack.class);
given(audioTrackSelector.selectAudioTrack(audioTrack, rendererTypeRequester)).willReturn(false);

boolean success = facade.selectAudioTrack(audioTrack);

assertThat(success).isFalse();
}

@Test
public void whenSelectingSubtitlesTrack_thenDelegatesToTrackSelector() {
PlayerSubtitleTrack subtitleTrack = mock(PlayerSubtitleTrack.class);

facade.selectSubtitleTrack(subtitleTrack);

verify(subtitleTrackSelector).selectTextTrack(subtitleTrack, rendererTypeRequester);
}

@Test
public void givenSelectingTextTrackSuceeds_whenSelectingSubtitlesTrack_thenReturnsTrue() {
PlayerSubtitleTrack subtitleTrack = mock(PlayerSubtitleTrack.class);
given(subtitleTrackSelector.selectTextTrack(subtitleTrack, rendererTypeRequester)).willReturn(true);

boolean success = facade.selectSubtitleTrack(subtitleTrack);

assertThat(success).isTrue();
}

@Test
public void givenSelectingTextTrackFails_whenSelectingSubtitlesTrack_thenReturnsFalse() {
PlayerSubtitleTrack subtitleTrack = mock(PlayerSubtitleTrack.class);
given(subtitleTrackSelector.selectTextTrack(subtitleTrack, rendererTypeRequester)).willReturn(false);

boolean success = facade.selectSubtitleTrack(subtitleTrack);

assertThat(success).isFalse();
}

@Test
public void whenGettingAudioTracks_thenDelegatesToTrackSelector() {
given(audioTrackSelector.getAudioTracks(any(RendererTypeRequester.class))).willReturn(AUDIO_TRACKS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,26 @@ public void whenSelectingSubtitlesTrack_thenShowsPlayerSubtitlesView() {
verify(playerView).showSubtitles();
}

@Test
public void givenSelectingSubtitleTrackSuceeds_whenSelectingSubtitlesTrack_thenReturnsTrue() {
PlayerSubtitleTrack playerSubtitleTrack = mock(PlayerSubtitleTrack.class);
given(exoPlayerFacade.selectSubtitleTrack(playerSubtitleTrack)).willReturn(true);

boolean success = player.showSubtitleTrack(playerSubtitleTrack);

assertThat(success).isTrue();
}

@Test
public void givenSelectingSubtitleTrackFails_whenSelectingSubtitlesTrack_thenReturnsFalse() {
PlayerSubtitleTrack playerSubtitleTrack = mock(PlayerSubtitleTrack.class);
given(exoPlayerFacade.selectSubtitleTrack(playerSubtitleTrack)).willReturn(false);

boolean success = player.showSubtitleTrack(playerSubtitleTrack);

assertThat(success).isFalse();
}

@Test
public void givenPlayerHasLoadedSubtitleCues_whenSelectingSubtitlesTrack_thenSetsSubtitleCuesOnView() {
TextCues textCues = givenPlayerHasLoadedSubtitleCues();
Expand Down

0 comments on commit 6794007

Please sign in to comment.