Commit 8b3025d4 by []inger Committed by Oliver Woodman

Make control view layout resource customizable.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137145209
parent ba6368a0
......@@ -24,7 +24,6 @@ import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import com.google.android.exoplayer2.C;
......@@ -38,6 +37,16 @@ import java.util.Locale;
/**
* A view to control video playback of an {@link ExoPlayer}.
*
* By setting the view attribute {@code controller_layout_id} a layout resource to use can
* be customized. All views are optional but if the buttons should have an appropriate logic
* assigned, the id of the views in the layout have to match the expected ids as follows:
*
* <ul>
* <li>Playback: {@code exo_play}, {@code exo_pause}, {@code exo_ffwd}, {@code exo_rew}.</li>
* <li>Progress: {@code exo_progress}, {@code exo_time_current}, {@code exo_time}.</li>
* <li>Playlist navigation: {@code exo_previous}, {@code exo_next}.</li>
* </ul>
*/
public class PlaybackControlView extends FrameLayout {
......@@ -63,12 +72,13 @@ public class PlaybackControlView extends FrameLayout {
private final ComponentListener componentListener;
private final View previousButton;
private final View nextButton;
private final ImageButton playButton;
private final View playButton;
private final View pauseButton;
private final View fastForwardButton;
private final View rewindButton;
private final TextView time;
private final TextView timeCurrent;
private final SeekBar progressBar;
private final View fastForwardButton;
private final View rewindButton;
private final StringBuilder formatBuilder;
private final Formatter formatter;
private final Timeline.Window currentWindow;
......@@ -108,6 +118,7 @@ public class PlaybackControlView extends FrameLayout {
public PlaybackControlView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
int layoutResourceId = R.layout.exo_playback_control_view;
rewindMs = DEFAULT_REWIND_MS;
fastForwardMs = DEFAULT_FAST_FORWARD_MS;
showTimeoutMs = DEFAULT_SHOW_TIMEOUT_MS;
......@@ -119,32 +130,49 @@ public class PlaybackControlView extends FrameLayout {
fastForwardMs = a.getInt(R.styleable.PlaybackControlView_fastforward_increment,
fastForwardMs);
showTimeoutMs = a.getInt(R.styleable.PlaybackControlView_show_timeout, showTimeoutMs);
layoutResourceId = a.getResourceId(R.styleable.PlaybackControlView_controller_layout_id,
layoutResourceId);
} finally {
a.recycle();
}
}
currentWindow = new Timeline.Window();
formatBuilder = new StringBuilder();
formatter = new Formatter(formatBuilder, Locale.getDefault());
componentListener = new ComponentListener();
LayoutInflater.from(context).inflate(R.layout.exo_playback_control_view, this);
time = (TextView) findViewById(R.id.time);
timeCurrent = (TextView) findViewById(R.id.time_current);
progressBar = (SeekBar) findViewById(R.id.mediacontroller_progress);
progressBar.setOnSeekBarChangeListener(componentListener);
progressBar.setMax(PROGRESS_BAR_MAX);
playButton = (ImageButton) findViewById(R.id.play);
playButton.setOnClickListener(componentListener);
previousButton = findViewById(R.id.prev);
previousButton.setOnClickListener(componentListener);
nextButton = findViewById(R.id.next);
nextButton.setOnClickListener(componentListener);
rewindButton = findViewById(R.id.rew);
rewindButton.setOnClickListener(componentListener);
fastForwardButton = findViewById(R.id.ffwd);
fastForwardButton.setOnClickListener(componentListener);
LayoutInflater.from(context).inflate(layoutResourceId, this);
time = (TextView) findViewById(R.id.exo_time);
timeCurrent = (TextView) findViewById(R.id.exo_time_current);
progressBar = (SeekBar) findViewById(R.id.exo_progress);
if (progressBar != null) {
progressBar.setOnSeekBarChangeListener(componentListener);
progressBar.setMax(PROGRESS_BAR_MAX);
}
playButton = findViewById(R.id.exo_play);
if (playButton != null) {
playButton.setOnClickListener(componentListener);
}
pauseButton = findViewById(R.id.exo_pause);
if (pauseButton != null) {
pauseButton.setOnClickListener(componentListener);
}
previousButton = findViewById(R.id.exo_prev);
if (previousButton != null) {
previousButton.setOnClickListener(componentListener);
}
nextButton = findViewById(R.id.exo_next);
if (nextButton != null) {
nextButton.setOnClickListener(componentListener);
}
rewindButton = findViewById(R.id.exo_rew);
if (rewindButton != null) {
rewindButton.setOnClickListener(componentListener);
}
fastForwardButton = findViewById(R.id.exo_ffwd);
if (fastForwardButton != null) {
fastForwardButton.setOnClickListener(componentListener);
}
}
/**
......@@ -285,11 +313,12 @@ public class PlaybackControlView extends FrameLayout {
return;
}
boolean playing = player != null && player.getPlayWhenReady();
String contentDescription = getResources().getString(
playing ? R.string.exo_controls_pause_description : R.string.exo_controls_play_description);
playButton.setContentDescription(contentDescription);
playButton.setImageResource(
playing ? R.drawable.exo_controls_pause : R.drawable.exo_controls_play);
if (playButton != null) {
playButton.setVisibility(playing ? GONE : VISIBLE);
}
if (pauseButton != null) {
pauseButton.setVisibility(playing ? VISIBLE : GONE);
}
}
private void updateNavigation() {
......@@ -313,7 +342,9 @@ public class PlaybackControlView extends FrameLayout {
setButtonEnabled(enableNext, nextButton);
setButtonEnabled(fastForwardMs > 0 && isSeekable, fastForwardButton);
setButtonEnabled(rewindMs > 0 && isSeekable, rewindButton);
progressBar.setEnabled(isSeekable);
if (progressBar != null) {
progressBar.setEnabled(isSeekable);
}
}
private void updateProgress() {
......@@ -322,16 +353,21 @@ public class PlaybackControlView extends FrameLayout {
}
long duration = player == null ? 0 : player.getDuration();
long position = player == null ? 0 : player.getCurrentPosition();
time.setText(stringForTime(duration));
if (!dragging) {
if (time != null) {
time.setText(stringForTime(duration));
}
if (timeCurrent != null && !dragging) {
timeCurrent.setText(stringForTime(position));
}
if (!dragging) {
progressBar.setProgress(progressBarValue(position));
if (progressBar != null) {
if (!dragging) {
progressBar.setProgress(progressBarValue(position));
}
long bufferedPosition = player == null ? 0 : player.getBufferedPosition();
progressBar.setSecondaryProgress(progressBarValue(bufferedPosition));
// Remove scheduled updates.
}
long bufferedPosition = player == null ? 0 : player.getBufferedPosition();
progressBar.setSecondaryProgress(progressBarValue(bufferedPosition));
// Remove scheduled updates.
removeCallbacks(updateProgressAction);
// Schedule an update if necessary.
int playbackState = player == null ? ExoPlayer.STATE_IDLE : player.getPlaybackState();
......@@ -350,6 +386,9 @@ public class PlaybackControlView extends FrameLayout {
}
private void setButtonEnabled(boolean enabled, View view) {
if (view == null) {
return;
}
view.setEnabled(enabled);
if (Util.SDK_INT >= 11) {
setViewAlphaV11(view, enabled ? 1f : 0.3f);
......@@ -500,7 +539,7 @@ public class PlaybackControlView extends FrameLayout {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
if (fromUser && timeCurrent != null) {
timeCurrent.setText(stringForTime(positionValue(progress)));
}
}
......@@ -508,7 +547,9 @@ public class PlaybackControlView extends FrameLayout {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
dragging = false;
player.seekTo(positionValue(seekBar.getProgress()));
if (player != null) {
player.seekTo(positionValue(seekBar.getProgress()));
}
hideAfterTimeout();
}
......@@ -542,17 +583,20 @@ public class PlaybackControlView extends FrameLayout {
@Override
public void onClick(View view) {
Timeline currentTimeline = player.getCurrentTimeline();
if (nextButton == view) {
next();
} else if (previousButton == view) {
previous();
} else if (fastForwardButton == view) {
fastForward();
} else if (rewindButton == view && currentTimeline != null) {
rewind();
} else if (playButton == view) {
player.setPlayWhenReady(!player.getPlayWhenReady());
if (player != null) {
if (nextButton == view) {
next();
} else if (previousButton == view) {
previous();
} else if (fastForwardButton == view) {
fastForward();
} else if (rewindButton == view) {
rewind();
} else if (playButton == view) {
player.setPlayWhenReady(true);
} else if (pauseButton == view) {
player.setPlayWhenReady(false);
}
}
hideAfterTimeout();
}
......
......@@ -90,23 +90,30 @@ public final class SimpleExoPlayerView extends FrameLayout {
LayoutInflater.from(context).inflate(R.layout.exo_simple_player_view, this);
componentListener = new ComponentListener();
layout = (AspectRatioFrameLayout) findViewById(R.id.video_frame);
layout = (AspectRatioFrameLayout) findViewById(R.id.exo_video_frame);
layout.setResizeMode(resizeMode);
shutterView = findViewById(R.id.shutter);
subtitleLayout = (SubtitleView) findViewById(R.id.subtitles);
shutterView = findViewById(R.id.exo_shutter);
subtitleLayout = (SubtitleView) findViewById(R.id.exo_subtitles);
subtitleLayout.setUserDefaultStyle();
subtitleLayout.setUserDefaultTextSize();
controller = (PlaybackControlView) findViewById(R.id.control);
controller.hide();
View controllerPlaceholder = findViewById(R.id.exo_controller_placeholder);
controller = new PlaybackControlView(context, attrs);
controller.setRewindIncrementMs(rewindMs);
controller.setFastForwardIncrementMs(fastForwardMs);
controller.setLayoutParams(controllerPlaceholder.getLayoutParams());
controller.hide();
this.controllerShowTimeoutMs = controllerShowTimeoutMs;
ViewGroup parent = ((ViewGroup) controllerPlaceholder.getParent());
int controllerIndex = parent.indexOfChild(controllerPlaceholder);
parent.removeView(controllerPlaceholder);
parent.addView(controller, controllerIndex);
View view = useTextureView ? new TextureView(context) : new SurfaceView(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);
surfaceView = view;
layout.addView(surfaceView, 0);
......
......@@ -14,7 +14,6 @@
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
......@@ -29,24 +28,22 @@
android:paddingTop="4dp"
android:orientation="horizontal">
<ImageButton android:id="@+id/prev"
android:contentDescription="@string/exo_controls_previous_description"
<ImageButton android:id="@+id/exo_prev"
style="@style/ExoMediaButton.Previous"/>
<ImageButton android:id="@+id/rew"
android:contentDescription="@string/exo_controls_rewind_description"
<ImageButton android:id="@+id/exo_rew"
style="@style/ExoMediaButton.Rewind"/>
<ImageButton android:id="@+id/play"
tools:ignore="ContentDescription"
style="@style/ExoMediaButton"/>
<ImageButton android:id="@+id/exo_play"
style="@style/ExoMediaButton.Play"/>
<ImageButton android:id="@+id/ffwd"
android:contentDescription="@string/exo_controls_fastforward_description"
<ImageButton android:id="@+id/exo_pause"
style="@style/ExoMediaButton.Pause"/>
<ImageButton android:id="@+id/exo_ffwd"
style="@style/ExoMediaButton.FastForward"/>
<ImageButton android:id="@+id/next"
android:contentDescription="@string/exo_controls_previous_description"
<ImageButton android:id="@+id/exo_next"
style="@style/ExoMediaButton.Next"/>
</LinearLayout>
......@@ -56,7 +53,7 @@
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/time_current"
<TextView android:id="@+id/exo_time_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
......@@ -67,13 +64,13 @@
android:paddingEnd="4dp"
android:textColor="#FFBEBEBE"/>
<SeekBar android:id="@+id/mediacontroller_progress"
<SeekBar android:id="@+id/exo_progress"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="32dp"
style="?android:attr/progressBarStyleHorizontal"/>
<TextView android:id="@+id/time"
<TextView android:id="@+id/exo_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
......
......@@ -17,23 +17,23 @@
android:layout_height="match_parent"
android:layout_width="match_parent">
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout android:id="@+id/video_frame"
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout android:id="@+id/exo_video_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<View android:id="@+id/shutter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>
<View android:id="@+id/exo_shutter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>
<com.google.android.exoplayer2.ui.SubtitleView android:id="@+id/subtitles"
<com.google.android.exoplayer2.ui.SubtitleView android:id="@+id/exo_subtitles"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
<com.google.android.exoplayer2.ui.PlaybackControlView android:id="@+id/control"
<View android:id="@+id/exo_controller_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
......
......@@ -23,6 +23,7 @@
<attr name="show_timeout" format="integer"/>
<attr name="rewind_increment" format="integer"/>
<attr name="fastforward_increment" format="integer"/>
<attr name="controller_layout_id" format="reference"/>
<declare-styleable name="SimpleExoPlayerView">
<attr name="use_controller" format="boolean"/>
......@@ -31,6 +32,7 @@
<attr name="rewind_increment"/>
<attr name="fastforward_increment"/>
<attr name="resize_mode"/>
<attr name="controller_layout_id"/>
</declare-styleable>
<declare-styleable name="AspectRatioFrameLayout">
......@@ -41,6 +43,7 @@
<attr name="show_timeout"/>
<attr name="rewind_increment"/>
<attr name="fastforward_increment"/>
<attr name="controller_layout_id"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<resources>
<item name="exo_play" type="id"/>
<item name="exo_pause" type="id"/>
<item name="exo_rew" type="id"/>
<item name="exo_ffwd" type="id"/>
<item name="exo_prev" type="id"/>
<item name="exo_next" type="id"/>
<item name="exo_time" type="id"/>
<item name="exo_time_current" type="id"/>
<item name="exo_progress" type="id"/>
</resources>
......@@ -41,4 +41,14 @@
<item name="android:contentDescription">@string/exo_controls_rewind_description</item>
</style>
<style name="ExoMediaButton.Play">
<item name="android:src">@drawable/exo_controls_play</item>
<item name="android:contentDescription">@string/exo_controls_play_description</item>
</style>
<style name="ExoMediaButton.Pause">
<item name="android:src">@drawable/exo_controls_pause</item>
<item name="android:contentDescription">@string/exo_controls_pause_description</item>
</style>
</resources>
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