From 464da703cad93b35514ca36a68b6ef075e7b04dd Mon Sep 17 00:00:00 2001 From: Robert Shih Date: Thu, 29 May 2014 10:54:32 -0700 Subject: [PATCH] MediaPlayer: added getSelectedTrack Change-Id: Ic39b9b3d5e72e13452320a0272deb489a0285913 --- api/current.txt | 2 + media/java/android/media/MediaPlayer.java | 46 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/api/current.txt b/api/current.txt index b8490c4f0fa39..c618b9bf21437 100644 --- a/api/current.txt +++ b/api/current.txt @@ -14668,6 +14668,7 @@ package android.media { method public int getAudioSessionId(); method public int getCurrentPosition(); method public int getDuration(); + method public int getSelectedTrack(int) throws java.lang.IllegalStateException; method public android.media.MediaPlayer.TrackInfo[] getTrackInfo() throws java.lang.IllegalStateException; method public int getVideoHeight(); method public int getVideoWidth(); @@ -14767,6 +14768,7 @@ package android.media { method public int getTrackType(); method public void writeToParcel(android.os.Parcel, int); field public static final int MEDIA_TRACK_TYPE_AUDIO = 2; // 0x2 + field public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4; // 0x4 field public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3; // 0x3 field public static final int MEDIA_TRACK_TYPE_UNKNOWN = 0; // 0x0 field public static final int MEDIA_TRACK_TYPE_VIDEO = 1; // 0x1 diff --git a/media/java/android/media/MediaPlayer.java b/media/java/android/media/MediaPlayer.java index e25714a53e286..7b49dd74beb44 100644 --- a/media/java/android/media/MediaPlayer.java +++ b/media/java/android/media/MediaPlayer.java @@ -631,6 +631,7 @@ public class MediaPlayer implements SubtitleController.Listener private static final int INVOKE_ID_SELECT_TRACK = 4; private static final int INVOKE_ID_DESELECT_TRACK = 5; private static final int INVOKE_ID_SET_VIDEO_SCALE_MODE = 6; + private static final int INVOKE_ID_GET_SELECTED_TRACK = 7; /** * Create a request parcel which can be routed to the native media @@ -1634,7 +1635,6 @@ public class MediaPlayer implements SubtitleController.Listener public static final int MEDIA_TRACK_TYPE_VIDEO = 1; public static final int MEDIA_TRACK_TYPE_AUDIO = 2; public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3; - /** @hide */ public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4; final int mTrackType; @@ -2059,6 +2059,7 @@ public class MediaPlayer implements SubtitleController.Listener throws IllegalArgumentException, IllegalStateException { if (!availableMimeTypeForExternalSource(mimeType)) { throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mimeType); + } Parcel request = Parcel.obtain(); @@ -2077,6 +2078,49 @@ public class MediaPlayer implements SubtitleController.Listener } } + /** + * Returns the index of the audio, video, or subtitle track currently selected for playback, + * The return value is an index into the array returned by {@link #getTrackInfo()}, and can + * be used in calls to {@link #selectTrack(int)} or {@link #deselectTrack(int)}. + * + * @param trackType should be one of {@link TrackInfo#MEDIA_TRACK_TYPE_VIDEO}, + * {@link TrackInfo#MEDIA_TRACK_TYPE_AUDIO}, or + * {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE} + * @return index of the audio, video, or subtitle track currently selected for playback; + * a negative integer is returned when there is no selected track for {@code trackType} or + * when {@code trackType} is not one of audio, video, or subtitle. + * @throws IllegalStateException if called after {@link #release()} + * + * @see {@link #getTrackInfo()} + * @see {@link #selectTrack(int)} + * @see {@link #deselectTrack(int)} + */ + public int getSelectedTrack(int trackType) throws IllegalStateException { + if (trackType == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE && mSubtitleController != null) { + SubtitleTrack subtitleTrack = mSubtitleController.getSelectedTrack(); + if (subtitleTrack != null) { + int index = mOutOfBandSubtitleTracks.indexOf(subtitleTrack); + if (index >= 0) { + return mInbandSubtitleTracks.length + index; + } + } + } + + Parcel request = Parcel.obtain(); + Parcel reply = Parcel.obtain(); + try { + request.writeInterfaceToken(IMEDIA_PLAYER); + request.writeInt(INVOKE_ID_GET_SELECTED_TRACK); + request.writeInt(trackType); + invoke(request, reply); + int selectedTrack = reply.readInt(); + return selectedTrack; + } finally { + request.recycle(); + reply.recycle(); + } + } + /** * Selects a track. *