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

Commit

Permalink
Merge pull request #13 from novoda/audio_described_track
Browse files Browse the repository at this point in the history
Select Audio Track
  • Loading branch information
juankysoriano authored Apr 7, 2017
2 parents 0d29c0e + 3fef8ed commit 21e8208
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 15
minSdkVersion 16
targetSdkVersion 24
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/com/novoda/noplayer/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.novoda.noplayer.exoplayer.Bitrate;
import com.novoda.noplayer.player.PlayerInformation;

import java.util.List;

public interface Player extends PlayerState, PlayerListeners {

void play();
Expand All @@ -27,6 +29,10 @@ public interface Player extends PlayerState, PlayerListeners {

void attach(PlayerView playerView);

void selectAudioTrack(int audioTrackIndex);

List<PlayerAudioTrack> getAudioTracks();

interface PreReleaseListener {

PreReleaseListener NULL_IMPL = new PreReleaseListener() {
Expand Down
75 changes: 75 additions & 0 deletions core/src/main/java/com/novoda/noplayer/PlayerAudioTrack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.novoda.noplayer;

public class PlayerAudioTrack {

private final String trackId;
private final String language;
private final String mimeType;
private final int numberOfChannels;
private final int frequency;

public PlayerAudioTrack(String trackId, String language, String mimeType, int numberOfChannels, int frequency) {
this.trackId = trackId;
this.language = language;
this.mimeType = mimeType;
this.numberOfChannels = numberOfChannels;
this.frequency = frequency;
}

public String trackId() {
return trackId;
}

public String language() {
return language;
}

public String mimeType() {
return mimeType;
}

public int numberOfChannels() {
return numberOfChannels;
}

public int frequency() {
return frequency;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

PlayerAudioTrack that = (PlayerAudioTrack) o;

if (numberOfChannels != that.numberOfChannels) {
return false;
}
if (frequency != that.frequency) {
return false;
}
if (trackId != null ? !trackId.equals(that.trackId) : that.trackId != null) {
return false;
}
if (language != null ? !language.equals(that.language) : that.language != null) {
return false;
}
return mimeType != null ? mimeType.equals(that.mimeType) : that.mimeType == null;

}

@Override
public int hashCode() {
int result = trackId != null ? trackId.hashCode() : 0;
result = 31 * result + (language != null ? language.hashCode() : 0);
result = 31 * result + (mimeType != null ? mimeType.hashCode() : 0);
result = 31 * result + numberOfChannels;
result = 31 * result + frequency;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer;
import com.google.android.exoplayer.MediaCodecTrackRenderer.DecoderInitializationException;
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer;
import com.google.android.exoplayer.MediaFormat;
import com.google.android.exoplayer.TimeRange;
import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.audio.AudioTrack;
Expand All @@ -24,11 +25,13 @@
import com.google.android.exoplayer.text.TextRenderer;
import com.google.android.exoplayer.upstream.BandwidthMeter;
import com.novoda.noplayer.ContentType;
import com.novoda.noplayer.PlayerAudioTrack;
import com.novoda.noplayer.SurfaceHolderRequester;
import com.novoda.notils.exception.DeveloperError;
import com.novoda.notils.logger.simple.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -435,6 +438,37 @@ public void onUpstreamDiscarded(int sourceId, long mediaStartTimeMs, long mediaE
// Do nothing.
}

public void selectAudioTrack(int audioTrackIndex) {
int trackCount = player.getTrackCount(Renderers.AUDIO_RENDERER_ID);
if (audioTrackIndex < 0 || audioTrackIndex > trackCount - 1) {
Log.e(String.format(
"Attempt to %s has been ignored because an invalid position was specified: %s, total: %s",
"selectAudioTrack()",
audioTrackIndex,
trackCount
)
);
return;
}
player.setSelectedTrack(Renderers.AUDIO_RENDERER_ID, audioTrackIndex);
}

public List<PlayerAudioTrack> getAudioTracks() {
List<PlayerAudioTrack> tracks = new ArrayList<>();
for (int i = 0; i < player.getTrackCount(Renderers.AUDIO_RENDERER_ID); i++) {
MediaFormat track = player.getTrackFormat(Renderers.AUDIO_RENDERER_ID, i);
PlayerAudioTrack playerAudioTrack = new PlayerAudioTrack(
track.trackId,
track.language,
track.mimeType,
track.channelCount,
track.bitrate
);
tracks.add(playerAudioTrack);
}
return tracks;
}

public void setSubtitleLayout(SubtitleLayout subtitleLayout) {
this.subtitleLayout = subtitleLayout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.novoda.noplayer.ContentType;
import com.novoda.noplayer.Heart;
import com.novoda.noplayer.Player;
import com.novoda.noplayer.PlayerAudioTrack;
import com.novoda.noplayer.PlayerListenersHolder;
import com.novoda.noplayer.PlayerView;
import com.novoda.noplayer.Timeout;
Expand All @@ -13,6 +14,8 @@
import com.novoda.noplayer.VideoPosition;
import com.novoda.noplayer.player.PlayerInformation;

import java.util.List;

public class ExoPlayerImpl extends PlayerListenersHolder implements Player {

private static final ExoPlayerInformation EXO_PLAYER_INFORMATION = new ExoPlayerInformation();
Expand Down Expand Up @@ -178,4 +181,14 @@ public void attach(PlayerView playerView) {
public ExoPlayerFacade getInternalExoPlayer() {
return exoPlayer;
}

@Override
public void selectAudioTrack(int audioTrackIndex) {
exoPlayer.selectAudioTrack(audioTrackIndex);
}

@Override
public List<PlayerAudioTrack> getAudioTracks() {
return exoPlayer.getAudioTracks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public TrackRenderer getVideoRenderer() {
return renderers[VIDEO_RENDERER_INDEX];
}

public TrackRenderer getAudioRenderer() {
return renderers[AUDIO_RENDERER_INDEX];
}

public TrackRenderer[] asArray() {
return Arrays.copyOf(renderers, renderers.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import android.net.Uri;
import android.view.SurfaceHolder;

import com.novoda.noplayer.PlayerAudioTrack;
import com.novoda.noplayer.SurfaceHolderRequester;
import com.novoda.notils.logger.simple.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AndroidMediaPlayerFacade {
Expand Down Expand Up @@ -73,7 +76,6 @@ private void requestAudioFocus() {

private MediaPlayer createMediaPlayer(SurfaceHolder surfaceHolder, Uri videoUri) throws IOException {
MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setOnPreparedListener(internalPeparedListener);
mediaPlayer.setOnVideoSizeChangedListener(internalSizeChangedListener);
mediaPlayer.setOnCompletionListener(internalCompletionListener);
Expand Down Expand Up @@ -236,4 +238,35 @@ private boolean isInPlaybackState() {
&& currentState != STATE_IDLE
&& currentState != STATE_PREPARING;
}

public List<PlayerAudioTrack> getAudioTracks() {
ArrayList<PlayerAudioTrack> audioTracks = new ArrayList<>();
for (MediaPlayer.TrackInfo trackInfo : mediaPlayer.getTrackInfo()) {
if (trackInfo.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
audioTracks.add(new PlayerAudioTrack(String.valueOf(trackInfo.hashCode()), trackInfo.getLanguage(), "", -1, -1));
}
}
return audioTracks;
}

public void setAudioTrack(int audioTrackIndex) {
int index = 0;
MediaPlayer.TrackInfo[] trackInfos = mediaPlayer.getTrackInfo();

for (int i = 0; i < trackInfos.length; i++) {
MediaPlayer.TrackInfo trackInfo = trackInfos[i];
if (trackInfo.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO && index == audioTrackIndex) {
mediaPlayer.selectTrack(i);
} else if (trackInfo.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
index++;
}
}
Log.e(String.format(
"Attempt to %s has been ignored because an invalid position was specified: %s, total: %s",
"selectAudioTrack()",
audioTrackIndex,
index
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.novoda.noplayer.ContentType;
import com.novoda.noplayer.Heart;
import com.novoda.noplayer.Player;
import com.novoda.noplayer.PlayerAudioTrack;
import com.novoda.noplayer.PlayerListenersHolder;
import com.novoda.noplayer.PlayerView;
import com.novoda.noplayer.SystemClock;
Expand All @@ -18,6 +19,8 @@
import com.novoda.noplayer.player.PlayerInformation;
import com.novoda.notils.logger.simple.Log;

import java.util.List;

public class AndroidMediaPlayerImpl extends PlayerListenersHolder implements Player {

private static final VideoPosition NO_SEEK_TO_POSITION = VideoPosition.INVALID;
Expand Down Expand Up @@ -301,4 +304,15 @@ public void release() {
getStateChangedListeners().onVideoReleased();
videoContainer.hide();
}

@Override
public void selectAudioTrack(int audioTrackIndex) {
mediaPlayer.setAudioTrack(audioTrackIndex);

}

@Override
public List<PlayerAudioTrack> getAudioTracks() {
return mediaPlayer.getAudioTracks();
}
}

0 comments on commit 21e8208

Please sign in to comment.