Commit f66b90e3 by olly Committed by Oliver Woodman

Eliminate cruft from the demo apps's PlayerActivity

Useful functionality promoted to core library:

1. Management of SurfaceHolder.Callback lifecycle
   promoted to SimpleExoPlayer
2. Ability to determine whether audio/video tracks
   exist but are all unsupported promoted to
   MappingTrackSelector.TrackInfo
3. Read external storage permissions check promoted
   to Util
4. SubtitleView given ability to act directly as a
   TextRenderer.Output to remove layer of indirection
5. SubtitleView given ability to configure itself to
   user's platform wide caption styling
6. KeyCompatibleMediaController promoted to library's
   UI package.

Relocation of boring stuff:

1. ID3 frame logging moved to EventLogger.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=128714230
parent 3501332d
...@@ -22,6 +22,13 @@ import com.google.android.exoplayer2.RendererCapabilities; ...@@ -22,6 +22,13 @@ import com.google.android.exoplayer2.RendererCapabilities;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.decoder.DecoderCounters; import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.drm.StreamingDrmSessionManager; import com.google.android.exoplayer2.drm.StreamingDrmSessionManager;
import com.google.android.exoplayer2.metadata.MetadataRenderer;
import com.google.android.exoplayer2.metadata.id3.ApicFrame;
import com.google.android.exoplayer2.metadata.id3.GeobFrame;
import com.google.android.exoplayer2.metadata.id3.Id3Frame;
import com.google.android.exoplayer2.metadata.id3.PrivFrame;
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame;
import com.google.android.exoplayer2.metadata.id3.TxxxFrame;
import com.google.android.exoplayer2.source.AdaptiveMediaSourceEventListener; import com.google.android.exoplayer2.source.AdaptiveMediaSourceEventListener;
import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.Timeline; import com.google.android.exoplayer2.source.Timeline;
...@@ -37,14 +44,16 @@ import android.util.Log; ...@@ -37,14 +44,16 @@ import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.List;
import java.util.Locale; import java.util.Locale;
/** /**
* Logs player events using {@link Log}. * Logs player events using {@link Log}.
*/ */
public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.DebugListener, /* package */ final class EventLogger implements ExoPlayer.EventListener,
AdaptiveMediaSourceEventListener, ExtractorMediaSource.EventListener, SimpleExoPlayer.DebugListener, AdaptiveMediaSourceEventListener,
StreamingDrmSessionManager.EventListener, MappingTrackSelector.EventListener { ExtractorMediaSource.EventListener, StreamingDrmSessionManager.EventListener,
MappingTrackSelector.EventListener, MetadataRenderer.Output<List<Id3Frame>> {
private static final String TAG = "EventLogger"; private static final String TAG = "EventLogger";
private static final NumberFormat TIME_FORMAT; private static final NumberFormat TIME_FORMAT;
...@@ -54,15 +63,10 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb ...@@ -54,15 +63,10 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb
TIME_FORMAT.setMaximumFractionDigits(2); TIME_FORMAT.setMaximumFractionDigits(2);
} }
private long sessionStartTimeMs; private final long startTimeMs;
public void startSession() { public EventLogger() {
sessionStartTimeMs = SystemClock.elapsedRealtime(); startTimeMs = SystemClock.elapsedRealtime();
Log.d(TAG, "start [0]");
}
public void endSession() {
Log.d(TAG, "end [" + getSessionTimeString() + "]");
} }
// ExoPlayer.EventListener // ExoPlayer.EventListener
...@@ -152,6 +156,36 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb ...@@ -152,6 +156,36 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb
Log.d(TAG, "]"); Log.d(TAG, "]");
} }
// MetadataRenderer.Output<List<Id3Frame>>
@Override
public void onMetadata(List<Id3Frame> id3Frames) {
for (Id3Frame id3Frame : id3Frames) {
if (id3Frame instanceof TxxxFrame) {
TxxxFrame txxxFrame = (TxxxFrame) id3Frame;
Log.i(TAG, String.format("ID3 TimedMetadata %s: description=%s, value=%s", txxxFrame.id,
txxxFrame.description, txxxFrame.value));
} else if (id3Frame instanceof PrivFrame) {
PrivFrame privFrame = (PrivFrame) id3Frame;
Log.i(TAG, String.format("ID3 TimedMetadata %s: owner=%s", privFrame.id, privFrame.owner));
} else if (id3Frame instanceof GeobFrame) {
GeobFrame geobFrame = (GeobFrame) id3Frame;
Log.i(TAG, String.format("ID3 TimedMetadata %s: mimeType=%s, filename=%s, description=%s",
geobFrame.id, geobFrame.mimeType, geobFrame.filename, geobFrame.description));
} else if (id3Frame instanceof ApicFrame) {
ApicFrame apicFrame = (ApicFrame) id3Frame;
Log.i(TAG, String.format("ID3 TimedMetadata %s: mimeType=%s, description=%s",
apicFrame.id, apicFrame.mimeType, apicFrame.description));
} else if (id3Frame instanceof TextInformationFrame) {
TextInformationFrame textInformationFrame = (TextInformationFrame) id3Frame;
Log.i(TAG, String.format("ID3 TimedMetadata %s: description=%s", textInformationFrame.id,
textInformationFrame.description));
} else {
Log.i(TAG, String.format("ID3 TimedMetadata %s", id3Frame.id));
}
}
}
// SimpleExoPlayer.DebugListener // SimpleExoPlayer.DebugListener
@Override @Override
...@@ -282,7 +316,7 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb ...@@ -282,7 +316,7 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb
} }
private String getSessionTimeString() { private String getSessionTimeString() {
return getTimeString(SystemClock.elapsedRealtime() - sessionStartTimeMs); return getTimeString(SystemClock.elapsedRealtime() - startTimeMs);
} }
private static String getTimeString(long timeMs) { private static String getTimeString(long timeMs) {
......
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
<string name="selection_default_none">Default (none)</string> <string name="selection_default_none">Default (none)</string>
<string name="unexpected_intent_action">Unexpected intent action: <xliff:g id="action">%1$s</xliff:g></string>
<string name="enable_random_adaptation">Enable random adaptation</string> <string name="enable_random_adaptation">Enable random adaptation</string>
<string name="error_drm_not_supported">Protected content not supported on API levels below 18</string> <string name="error_drm_not_supported">Protected content not supported on API levels below 18</string>
......
...@@ -41,6 +41,7 @@ import android.media.PlaybackParams; ...@@ -41,6 +41,7 @@ import android.media.PlaybackParams;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceHolder;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -82,20 +83,6 @@ public final class SimpleExoPlayer implements ExoPlayer { ...@@ -82,20 +83,6 @@ public final class SimpleExoPlayer implements ExoPlayer {
void onAudioTrackUnderrun(int bufferSize, long bufferSizeMs, long elapsedSinceLastFeedMs); void onAudioTrackUnderrun(int bufferSize, long bufferSizeMs, long elapsedSinceLastFeedMs);
} }
/**
* A listener for receiving notifications of timed text.
*/
public interface CaptionListener {
void onCues(List<Cue> cues);
}
/**
* A listener for receiving ID3 metadata parsed from the media stream.
*/
public interface Id3MetadataListener {
void onId3Metadata(List<Id3Frame> id3Frames);
}
private static final String TAG = "SimpleExoPlayer"; private static final String TAG = "SimpleExoPlayer";
private static final int MAX_DROPPED_VIDEO_FRAME_COUNT_TO_NOTIFY = 50; private static final int MAX_DROPPED_VIDEO_FRAME_COUNT_TO_NOTIFY = 50;
...@@ -109,8 +96,9 @@ public final class SimpleExoPlayer implements ExoPlayer { ...@@ -109,8 +96,9 @@ public final class SimpleExoPlayer implements ExoPlayer {
private Format videoFormat; private Format videoFormat;
private Format audioFormat; private Format audioFormat;
private CaptionListener captionListener; private SurfaceHolder surfaceHolder;
private Id3MetadataListener id3MetadataListener; private TextRenderer.Output textOutput;
private MetadataRenderer.Output<List<Id3Frame>> id3Output;
private VideoListener videoListener; private VideoListener videoListener;
private DebugListener debugListener; private DebugListener debugListener;
private DecoderCounters videoDecoderCounters; private DecoderCounters videoDecoderCounters;
...@@ -176,23 +164,40 @@ public final class SimpleExoPlayer implements ExoPlayer { ...@@ -176,23 +164,40 @@ public final class SimpleExoPlayer implements ExoPlayer {
} }
/** /**
* Sets the {@link Surface} onto which video will be rendered. * Sets the {@link Surface} onto which video will be rendered. The caller is responsible for
* tracking the lifecycle of the surface, and must clear the surface by calling
* {@code setVideoSurface(null)} if the surface is destroyed.
* <p>
* If the surface is held by a {@link SurfaceHolder} then it's recommended to use
* {@link #setVideoSurfaceHolder(SurfaceHolder)} rather than this method, since passing the
* holder allows the player to track the lifecycle of the surface automatically.
* *
* @param surface The {@link Surface}. * @param surface The {@link Surface}.
*/ */
public void setSurface(Surface surface) { public void setVideoSurface(Surface surface) {
ExoPlayerMessage[] messages = new ExoPlayerMessage[videoRendererCount]; if (surfaceHolder != null) {
int count = 0; surfaceHolder.removeCallback(componentListener);
for (Renderer renderer : renderers) { surfaceHolder = null;
if (renderer.getTrackType() == C.TRACK_TYPE_VIDEO) {
messages[count++] = new ExoPlayerMessage(renderer, C.MSG_SET_SURFACE, surface);
}
} }
if (surface == null) { setVideoSurfaceInternal(surface);
// Block to ensure that the surface is not accessed after the method returns. }
player.blockingSendMessages(messages);
/**
* Sets the {@link SurfaceHolder} that holds the {@link Surface} onto which video will be
* rendered. The player will track the lifecycle of the surface automatically.
*
* @param surfaceHolder The surface holder.
*/
public void setVideoSurfaceHolder(SurfaceHolder surfaceHolder) {
if (this.surfaceHolder != null) {
this.surfaceHolder.removeCallback(componentListener);
}
this.surfaceHolder = surfaceHolder;
if (surfaceHolder == null) {
setVideoSurfaceInternal(null);
} else { } else {
player.sendMessages(messages); setVideoSurfaceInternal(surfaceHolder.getSurface());
surfaceHolder.addCallback(componentListener);
} }
} }
...@@ -287,21 +292,21 @@ public final class SimpleExoPlayer implements ExoPlayer { ...@@ -287,21 +292,21 @@ public final class SimpleExoPlayer implements ExoPlayer {
} }
/** /**
* Sets a listener to receive caption events. * Sets an output to receive text events.
* *
* @param listener The listener. * @param output The output.
*/ */
public void setCaptionListener(CaptionListener listener) { public void setTextOutput(TextRenderer.Output output) {
captionListener = listener; textOutput = output;
} }
/** /**
* Sets a listener to receive metadata events. * Sets a listener to receive ID3 metadata events.
* *
* @param listener The listener. * @param output The output.
*/ */
public void setMetadataListener(Id3MetadataListener listener) { public void setId3Output(MetadataRenderer.Output<List<Id3Frame>> output) {
id3MetadataListener = listener; id3Output = output;
} }
// ExoPlayer implementation // ExoPlayer implementation
...@@ -488,8 +493,25 @@ public final class SimpleExoPlayer implements ExoPlayer { ...@@ -488,8 +493,25 @@ public final class SimpleExoPlayer implements ExoPlayer {
} }
} }
private void setVideoSurfaceInternal(Surface surface) {
ExoPlayerMessage[] messages = new ExoPlayerMessage[videoRendererCount];
int count = 0;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_VIDEO) {
messages[count++] = new ExoPlayerMessage(renderer, C.MSG_SET_SURFACE, surface);
}
}
if (surface == null) {
// Block to ensure that the surface is not accessed after the method returns.
player.blockingSendMessages(messages);
} else {
player.sendMessages(messages);
}
}
private final class ComponentListener implements VideoRendererEventListener, private final class ComponentListener implements VideoRendererEventListener,
AudioRendererEventListener, TextRenderer.Output, MetadataRenderer.Output<List<Id3Frame>> { AudioRendererEventListener, TextRenderer.Output, MetadataRenderer.Output<List<Id3Frame>>,
SurfaceHolder.Callback {
// VideoRendererEventListener implementation // VideoRendererEventListener implementation
...@@ -603,21 +625,42 @@ public final class SimpleExoPlayer implements ExoPlayer { ...@@ -603,21 +625,42 @@ public final class SimpleExoPlayer implements ExoPlayer {
audioSessionId = AudioTrack.SESSION_ID_NOT_SET; audioSessionId = AudioTrack.SESSION_ID_NOT_SET;
} }
// TextRendererOutput implementation // TextRenderer.Output implementation
@Override @Override
public void onCues(List<Cue> cues) { public void onCues(List<Cue> cues) {
if (captionListener != null) { if (textOutput != null) {
captionListener.onCues(cues); textOutput.onCues(cues);
} }
} }
// MetadataRenderer implementation // MetadataRenderer.Output<List<Id3Frame>> implementation
@Override @Override
public void onMetadata(List<Id3Frame> id3Frames) { public void onMetadata(List<Id3Frame> id3Frames) {
if (id3MetadataListener != null) { if (id3Output != null) {
id3MetadataListener.onId3Metadata(id3Frames); id3Output.onMetadata(id3Frames);
}
}
// SurfaceHolder.Callback implementation
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (player != null) {
setVideoSurfaceInternal(holder.getSurface());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Do nothing.
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (player != null) {
setVideoSurfaceInternal(null);
} }
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package com.google.android.exoplayer2.trackselection; package com.google.android.exoplayer2.trackselection;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.RendererCapabilities; import com.google.android.exoplayer2.RendererCapabilities;
import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
...@@ -258,11 +257,13 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -258,11 +257,13 @@ public abstract class MappingTrackSelector extends TrackSelector {
// Create a track group array for each renderer, and trim each rendererFormatSupports entry. // Create a track group array for each renderer, and trim each rendererFormatSupports entry.
TrackGroupArray[] rendererTrackGroupArrays = new TrackGroupArray[rendererCapabilities.length]; TrackGroupArray[] rendererTrackGroupArrays = new TrackGroupArray[rendererCapabilities.length];
int[] rendererTrackTypes = new int[rendererCapabilities.length];
for (int i = 0; i < rendererCapabilities.length; i++) { for (int i = 0; i < rendererCapabilities.length; i++) {
int rendererTrackGroupCount = rendererTrackGroupCounts[i]; int rendererTrackGroupCount = rendererTrackGroupCounts[i];
rendererTrackGroupArrays[i] = new TrackGroupArray( rendererTrackGroupArrays[i] = new TrackGroupArray(
Arrays.copyOf(rendererTrackGroups[i], rendererTrackGroupCount)); Arrays.copyOf(rendererTrackGroups[i], rendererTrackGroupCount));
rendererFormatSupports[i] = Arrays.copyOf(rendererFormatSupports[i], rendererTrackGroupCount); rendererFormatSupports[i] = Arrays.copyOf(rendererFormatSupports[i], rendererTrackGroupCount);
rendererTrackTypes[i] = rendererCapabilities[i].getTrackType();
} }
// Create a track group array for track groups not associated with a renderer. // Create a track group array for track groups not associated with a renderer.
...@@ -289,8 +290,9 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -289,8 +290,9 @@ public abstract class MappingTrackSelector extends TrackSelector {
// Package up the track information and selections. // Package up the track information and selections.
TrackSelectionArray trackSelectionArray = new TrackSelectionArray(trackSelections); TrackSelectionArray trackSelectionArray = new TrackSelectionArray(trackSelections);
TrackInfo trackInfo = new TrackInfo(rendererTrackGroupArrays, trackSelections, TrackInfo trackInfo = new TrackInfo(rendererTrackTypes, rendererTrackGroupArrays,
mixedMimeTypeAdaptationSupport, rendererFormatSupports, unassociatedTrackGroupArray); trackSelections, mixedMimeTypeAdaptationSupport, rendererFormatSupports,
unassociatedTrackGroupArray);
return Pair.<TrackSelectionArray, Object>create(trackSelectionArray, trackInfo); return Pair.<TrackSelectionArray, Object>create(trackSelectionArray, trackInfo);
} }
...@@ -352,13 +354,13 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -352,13 +354,13 @@ public abstract class MappingTrackSelector extends TrackSelector {
} }
/** /**
* Calls {@link RendererCapabilities#supportsFormat(Format)} for each track in the specified * Calls {@link RendererCapabilities#supportsFormat} for each track in the specified
* {@link TrackGroup}, returning the results in an array. * {@link TrackGroup}, returning the results in an array.
* *
* @param rendererCapabilities The {@link RendererCapabilities} of the renderer. * @param rendererCapabilities The {@link RendererCapabilities} of the renderer.
* @param group The {@link TrackGroup} to evaluate. * @param group The {@link TrackGroup} to evaluate.
* @return An array containing the result of calling * @return An array containing the result of calling
* {@link RendererCapabilities#supportsFormat(Format)} for each track in the group. * {@link RendererCapabilities#supportsFormat} for each track in the group.
* @throws ExoPlaybackException If an error occurs determining the format support. * @throws ExoPlaybackException If an error occurs determining the format support.
*/ */
private static int[] getFormatSupport(RendererCapabilities rendererCapabilities, TrackGroup group) private static int[] getFormatSupport(RendererCapabilities rendererCapabilities, TrackGroup group)
...@@ -424,6 +426,7 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -424,6 +426,7 @@ public abstract class MappingTrackSelector extends TrackSelector {
*/ */
public final int rendererCount; public final int rendererCount;
private final int[] rendererTrackTypes;
private final TrackGroupArray[] trackGroups; private final TrackGroupArray[] trackGroups;
private final TrackSelection[] trackSelections; private final TrackSelection[] trackSelections;
private final int[] mixedMimeTypeAdaptiveSupport; private final int[] mixedMimeTypeAdaptiveSupport;
...@@ -431,17 +434,19 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -431,17 +434,19 @@ public abstract class MappingTrackSelector extends TrackSelector {
private final TrackGroupArray unassociatedTrackGroups; private final TrackGroupArray unassociatedTrackGroups;
/** /**
* @param rendererTrackTypes The track type supported by each renderer.
* @param trackGroups The {@link TrackGroupArray}s for each renderer. * @param trackGroups The {@link TrackGroupArray}s for each renderer.
* @param trackSelections The current {@link TrackSelection}s for each renderer. * @param trackSelections The current {@link TrackSelection}s for each renderer.
* @param mixedMimeTypeAdaptiveSupport The result of * @param mixedMimeTypeAdaptiveSupport The result of
* {@link RendererCapabilities#supportsMixedMimeTypeAdaptation()} for each renderer. * {@link RendererCapabilities#supportsMixedMimeTypeAdaptation()} for each renderer.
* @param formatSupport The result of {@link RendererCapabilities#supportsFormat(Format)} for * @param formatSupport The result of {@link RendererCapabilities#supportsFormat} for each
* each track, indexed by renderer index, group index and track index (in that order). * track, indexed by renderer index, group index and track index (in that order).
* @param unassociatedTrackGroups Contains {@link TrackGroup}s not associated with any renderer. * @param unassociatedTrackGroups Contains {@link TrackGroup}s not associated with any renderer.
*/ */
/* package */ TrackInfo(TrackGroupArray[] trackGroups, TrackSelection[] trackSelections, /* package */ TrackInfo(int[] rendererTrackTypes, TrackGroupArray[] trackGroups,
int[] mixedMimeTypeAdaptiveSupport, int[][][] formatSupport, TrackSelection[] trackSelections, int[] mixedMimeTypeAdaptiveSupport,
TrackGroupArray unassociatedTrackGroups) { int[][][] formatSupport, TrackGroupArray unassociatedTrackGroups) {
this.rendererTrackTypes = rendererTrackTypes;
this.trackGroups = trackGroups; this.trackGroups = trackGroups;
this.trackSelections = trackSelections; this.trackSelections = trackSelections;
this.formatSupport = formatSupport; this.formatSupport = formatSupport;
...@@ -586,6 +591,25 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -586,6 +591,25 @@ public abstract class MappingTrackSelector extends TrackSelector {
return unassociatedTrackGroups; return unassociatedTrackGroups;
} }
/**
* Returns true if tracks of the specified type exist and have been associated with renderers,
* but are all unplayable. Returns false in all other cases.
*
* @param trackType The track type.
* @return True if tracks of the specified type exist, if at least one renderer exists that
* handles tracks of the specified type, and if all of the tracks if the specified type are
* unplayable. False in all other cases.
*/
public boolean hasOnlyUnplayableTracks(int trackType) {
int rendererSupport = TrackInfo.RENDERER_SUPPORT_NO_TRACKS;
for (int i = 0; i < rendererCount; i++) {
if (rendererTrackTypes[i] == trackType) {
rendererSupport = Math.max(rendererSupport, getRendererSupport(i));
}
}
return rendererSupport == RENDERER_SUPPORT_UNPLAYABLE_TRACKS;
}
} }
} }
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.ui;
import android.content.Context;
import android.view.KeyEvent;
import android.widget.MediaController;
/**
* An extension of {@link MediaController} with enhanced support for D-pad and media keys.
*/
public class KeyCompatibleMediaController extends MediaController {
private MediaController.MediaPlayerControl playerControl;
public KeyCompatibleMediaController(Context context) {
super(context);
}
@Override
public void setMediaPlayer(MediaController.MediaPlayerControl playerControl) {
super.setMediaPlayer(playerControl);
this.playerControl = playerControl;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
if (playerControl.canSeekForward() && (keyCode == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD
|| keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
playerControl.seekTo(playerControl.getCurrentPosition() + 15000); // milliseconds
show();
}
return true;
} else if (playerControl.canSeekBackward() && (keyCode == KeyEvent.KEYCODE_MEDIA_REWIND
|| keyCode == KeyEvent.KEYCODE_DPAD_LEFT)) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
playerControl.seekTo(playerControl.getCurrentPosition() - 5000); // milliseconds
show();
}
return true;
}
return super.dispatchKeyEvent(event);
}
}
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.ui;
import com.google.android.exoplayer2.ExoPlayer;
import android.view.View;
import android.view.View.OnClickListener;
/**
* An {@link OnClickListener} that can be passed to
* {@link android.widget.MediaController#setPrevNextListeners(OnClickListener, OnClickListener)} to
* make the controller's "previous" and "next" buttons visible and seek to the previous and next
* periods in the timeline of the media being played.
*/
public class MediaControllerPrevNextClickListener implements OnClickListener {
/**
* If a previous button is clicked the player is seeked to the start of the previous period if the
* playback position in the current period is less than or equal to this constant (and if a
* previous period exists). Else the player is seeked to the start of the current period.
*/
private static final long MAX_POSITION_FOR_SEEK_TO_PREVIOUS_PERIOD = 3000;
private final ExoPlayer player;
private final boolean isNext;
/**
* @param player The player to operate on.
* @param isNext True if this instance if for the "next" button. False for "previous".
*/
public MediaControllerPrevNextClickListener(ExoPlayer player, boolean isNext) {
this.player = player;
this.isNext = isNext;
}
@Override
public void onClick(View v) {
int currentPeriodIndex = player.getCurrentPeriodIndex();
if (isNext) {
if (currentPeriodIndex < player.getCurrentTimeline().getPeriodCount() - 1) {
player.seekTo(currentPeriodIndex + 1, 0);
}
} else {
if (currentPeriodIndex > 0
&& player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS_PERIOD) {
player.seekTo(currentPeriodIndex - 1, 0);
} else {
player.seekTo(currentPeriodIndex, 0);
}
}
}
}
...@@ -17,13 +17,17 @@ package com.google.android.exoplayer2.ui; ...@@ -17,13 +17,17 @@ package com.google.android.exoplayer2.ui;
import com.google.android.exoplayer2.text.CaptionStyleCompat; import com.google.android.exoplayer2.text.CaptionStyleCompat;
import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.TextRenderer;
import com.google.android.exoplayer2.util.Util;
import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.accessibility.CaptioningManager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -31,7 +35,7 @@ import java.util.List; ...@@ -31,7 +35,7 @@ import java.util.List;
/** /**
* A view for displaying subtitle {@link Cue}s. * A view for displaying subtitle {@link Cue}s.
*/ */
public final class SubtitleView extends View { public final class SubtitleView extends View implements TextRenderer.Output {
/** /**
* The default fractional text size. * The default fractional text size.
...@@ -75,6 +79,11 @@ public final class SubtitleView extends View { ...@@ -75,6 +79,11 @@ public final class SubtitleView extends View {
bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION; bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION;
} }
@Override
public void onCues(List<Cue> cues) {
setCues(cues);
}
/** /**
* Sets the cues to be displayed by the view. * Sets the cues to be displayed by the view.
* *
...@@ -114,6 +123,15 @@ public final class SubtitleView extends View { ...@@ -114,6 +123,15 @@ public final class SubtitleView extends View {
} }
/** /**
* Sets the text size to one derived from {@link CaptioningManager#getFontScale()}, or to a
* default size on API level 19 and earlier.
*/
public void setUserDefaultTextSize() {
float fontScale = Util.SDK_INT >= 19 ? getUserCaptionFontScaleV19() : 1f;
setFractionalTextSize(DEFAULT_TEXT_SIZE_FRACTION * fontScale);
}
/**
* Sets the text size to be a fraction of the view's remaining height after its top and bottom * Sets the text size to be a fraction of the view's remaining height after its top and bottom
* padding have been subtracted. * padding have been subtracted.
* <p> * <p>
...@@ -163,6 +181,14 @@ public final class SubtitleView extends View { ...@@ -163,6 +181,14 @@ public final class SubtitleView extends View {
} }
/** /**
* Sets the caption style to be equivalent to the one returned by
* {@link CaptioningManager#getUserStyle()}, or to a default style on API level 19 and earlier.
*/
public void setUserDefaultStyle() {
setStyle(Util.SDK_INT >= 19 ? getUserCaptionStyleV19() : CaptionStyleCompat.DEFAULT);
}
/**
* Sets the caption style. * Sets the caption style.
* *
* @param style A style for the view. * @param style A style for the view.
...@@ -223,4 +249,18 @@ public final class SubtitleView extends View { ...@@ -223,4 +249,18 @@ public final class SubtitleView extends View {
} }
} }
@TargetApi(19)
private float getUserCaptionFontScaleV19() {
CaptioningManager captioningManager =
(CaptioningManager) getContext().getSystemService(Context.CAPTIONING_SERVICE);
return captioningManager.getFontScale();
}
@TargetApi(19)
private CaptionStyleCompat getUserCaptionStyleV19() {
CaptioningManager captioningManager =
(CaptioningManager) getContext().getSystemService(Context.CAPTIONING_SERVICE);
return CaptionStyleCompat.createFromCaptionStyle(captioningManager.getUserStyle());
}
} }
...@@ -20,8 +20,12 @@ import com.google.android.exoplayer2.ExoPlayerLibraryInfo; ...@@ -20,8 +20,12 @@ import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DataSpec;
import android.Manifest.permission;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
...@@ -130,6 +134,31 @@ public final class Util { ...@@ -130,6 +134,31 @@ public final class Util {
} }
/** /**
* Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE}
* permission read the specified {@link Uri}s, requesting the permission if necessary.
*
* @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read.
* @return Whether a permission request was made.
*/
@TargetApi(23)
public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) {
if (Util.SDK_INT < 23) {
return false;
}
for (Uri uri : uris) {
if (Util.isLocalFileUri(uri)) {
if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE}, 0);
return true;
}
break;
}
}
return false;
}
/**
* Returns true if the URI is a path to a local file or a reference to a local file. * Returns true if the URI is a path to a local file or a reference to a local file.
* *
* @param uri The uri to test. * @param uri The uri to test.
......
...@@ -302,7 +302,7 @@ public abstract class ExoHostedTest implements HostedTest, ExoPlayer.EventListen ...@@ -302,7 +302,7 @@ public abstract class ExoHostedTest implements HostedTest, ExoPlayer.EventListen
MappingTrackSelector trackSelector, DrmSessionManager drmSessionManager) { MappingTrackSelector trackSelector, DrmSessionManager drmSessionManager) {
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(host, trackSelector, SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(host, trackSelector,
new DefaultLoadControl(), drmSessionManager); new DefaultLoadControl(), drmSessionManager);
player.setSurface(surface); player.setVideoSurface(surface);
return player; return player;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment