| 1 |
/* |
| 2 |
* Copyright (C) 2014 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 |
* in compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 |
* or implied. See the License for the specific language governing permissions and limitations under |
| 12 |
* the License. |
| 13 |
*/ |
| 14 |
|
| 15 |
package ${packageName}; |
| 16 |
|
| 17 |
import android.app.Activity; |
| 18 |
import android.graphics.Bitmap; |
| 19 |
import android.media.MediaMetadata; |
| 20 |
import android.media.MediaPlayer; |
| 21 |
import android.media.session.MediaSession; |
| 22 |
import android.media.session.PlaybackState; |
| 23 |
import android.net.Uri; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.view.KeyEvent; |
| 26 |
import android.widget.VideoView; |
| 27 |
|
| 28 |
import com.bumptech.glide.Glide; |
| 29 |
import com.bumptech.glide.request.animation.GlideAnimation; |
| 30 |
import com.bumptech.glide.request.target.SimpleTarget; |
| 31 |
|
| 32 |
/** |
| 33 |
* PlaybackOverlayActivity for video playback that loads PlaybackOverlayFragment |
| 34 |
*/ |
| 35 |
public class PlaybackOverlayActivity extends Activity implements |
| 36 |
PlaybackOverlayFragment.OnPlayPauseClickedListener { |
| 37 |
private static final String TAG = "PlaybackOverlayActivity"; |
| 38 |
|
| 39 |
private VideoView mVideoView; |
| 40 |
private LeanbackPlaybackState mPlaybackState = LeanbackPlaybackState.IDLE; |
| 41 |
private MediaSession mSession; |
| 42 |
|
| 43 |
/** |
| 44 |
* Called when the activity is first created. |
| 45 |
*/ |
| 46 |
@Override |
| 47 |
public void onCreate(Bundle savedInstanceState) { |
| 48 |
super.onCreate(savedInstanceState); |
| 49 |
setContentView(R.layout.playback_controls); |
| 50 |
loadViews(); |
| 51 |
setupCallbacks(); |
| 52 |
mSession = new MediaSession (this, "LeanbackSampleApp"); |
| 53 |
mSession.setCallback(new MediaSessionCallback()); |
| 54 |
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | |
| 55 |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); |
| 56 |
|
| 57 |
mSession.setActive(true); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void onDestroy() { |
| 62 |
super.onDestroy(); |
| 63 |
mVideoView.suspend(); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 68 |
PlaybackOverlayFragment playbackOverlayFragment = (PlaybackOverlayFragment) getFragmentManager().findFragmentById(R.id.playback_controls_fragment); |
| 69 |
switch (keyCode) { |
| 70 |
case KeyEvent.KEYCODE_MEDIA_PLAY: |
| 71 |
playbackOverlayFragment.togglePlayback(false); |
| 72 |
return true; |
| 73 |
case KeyEvent.KEYCODE_MEDIA_PAUSE: |
| 74 |
playbackOverlayFragment.togglePlayback(false); |
| 75 |
return true; |
| 76 |
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: |
| 77 |
if (mPlaybackState == LeanbackPlaybackState.PLAYING) { |
| 78 |
playbackOverlayFragment.togglePlayback(false); |
| 79 |
} else { |
| 80 |
playbackOverlayFragment.togglePlayback(true); |
| 81 |
} |
| 82 |
return true; |
| 83 |
default: |
| 84 |
return super.onKeyUp(keyCode, event); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Implementation of OnPlayPauseClickedListener |
| 90 |
*/ |
| 91 |
public void onFragmentPlayPause(Movie movie, int position, Boolean playPause) { |
| 92 |
mVideoView.setVideoPath(movie.getVideoUrl()); |
| 93 |
|
| 94 |
if (position == 0 || mPlaybackState == LeanbackPlaybackState.IDLE) { |
| 95 |
setupCallbacks(); |
| 96 |
mPlaybackState = LeanbackPlaybackState.IDLE; |
| 97 |
} |
| 98 |
|
| 99 |
if (playPause && mPlaybackState != LeanbackPlaybackState.PLAYING) { |
| 100 |
mPlaybackState = LeanbackPlaybackState.PLAYING; |
| 101 |
if (position > 0) { |
| 102 |
mVideoView.seekTo(position); |
| 103 |
mVideoView.start(); |
| 104 |
} |
| 105 |
} else { |
| 106 |
mPlaybackState = LeanbackPlaybackState.PAUSED; |
| 107 |
mVideoView.pause(); |
| 108 |
} |
| 109 |
updatePlaybackState(position); |
| 110 |
updateMetadata(movie); |
| 111 |
} |
| 112 |
|
| 113 |
private void updatePlaybackState(int position) { |
| 114 |
PlaybackState.Builder stateBuilder = new PlaybackState.Builder() |
| 115 |
.setActions(getAvailableActions()); |
| 116 |
int state = PlaybackState.STATE_PLAYING; |
| 117 |
if (mPlaybackState == LeanbackPlaybackState.PAUSED) { |
| 118 |
state = PlaybackState.STATE_PAUSED; |
| 119 |
} |
| 120 |
stateBuilder.setState(state, position, 1.0f); |
| 121 |
mSession.setPlaybackState(stateBuilder.build()); |
| 122 |
} |
| 123 |
|
| 124 |
private long getAvailableActions() { |
| 125 |
long actions = PlaybackState.ACTION_PLAY | |
| 126 |
PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | |
| 127 |
PlaybackState.ACTION_PLAY_FROM_SEARCH; |
| 128 |
|
| 129 |
if (mPlaybackState == LeanbackPlaybackState.PLAYING) { |
| 130 |
actions |= PlaybackState.ACTION_PAUSE; |
| 131 |
} |
| 132 |
|
| 133 |
return actions; |
| 134 |
} |
| 135 |
|
| 136 |
private void updateMetadata(final Movie movie) { |
| 137 |
final MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder(); |
| 138 |
|
| 139 |
String title = movie.getTitle().replace("_", " -"); |
| 140 |
|
| 141 |
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, title); |
| 142 |
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, |
| 143 |
movie.getDescription()); |
| 144 |
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, |
| 145 |
movie.getCardImageUrl()); |
| 146 |
|
| 147 |
// And at minimum the title and artist for legacy support |
| 148 |
metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, title); |
| 149 |
metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, movie.getStudio()); |
| 150 |
|
| 151 |
Glide.with(this) |
| 152 |
.load(Uri.parse(movie.getCardImageUrl())) |
| 153 |
.asBitmap() |
| 154 |
.into(new SimpleTarget<Bitmap>(500, 500) { |
| 155 |
@Override |
| 156 |
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { |
| 157 |
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap); |
| 158 |
mSession.setMetadata(metadataBuilder.build()); |
| 159 |
} |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
private void loadViews() { |
| 164 |
mVideoView = (VideoView) findViewById(R.id.videoView); |
| 165 |
mVideoView.setFocusable(false); |
| 166 |
mVideoView.setFocusableInTouchMode(false); |
| 167 |
} |
| 168 |
|
| 169 |
private void setupCallbacks() { |
| 170 |
|
| 171 |
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { |
| 172 |
|
| 173 |
@Override |
| 174 |
public boolean onError(MediaPlayer mp, int what, int extra) { |
| 175 |
String msg = ""; |
| 176 |
if (extra == MediaPlayer.MEDIA_ERROR_TIMED_OUT) { |
| 177 |
msg = getString(R.string.video_error_media_load_timeout); |
| 178 |
} else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { |
| 179 |
msg = getString(R.string.video_error_server_inaccessible); |
| 180 |
} else { |
| 181 |
msg = getString(R.string.video_error_unknown_error); |
| 182 |
} |
| 183 |
mVideoView.stopPlayback(); |
| 184 |
mPlaybackState = LeanbackPlaybackState.IDLE; |
| 185 |
return false; |
| 186 |
} |
| 187 |
}); |
| 188 |
|
| 189 |
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
| 190 |
@Override |
| 191 |
public void onPrepared(MediaPlayer mp) { |
| 192 |
if (mPlaybackState == LeanbackPlaybackState.PLAYING) { |
| 193 |
mVideoView.start(); |
| 194 |
} |
| 195 |
} |
| 196 |
}); |
| 197 |
|
| 198 |
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { |
| 199 |
@Override |
| 200 |
public void onCompletion(MediaPlayer mp) { |
| 201 |
mPlaybackState = LeanbackPlaybackState.IDLE; |
| 202 |
} |
| 203 |
}); |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public void onResume() { |
| 209 |
super.onResume(); |
| 210 |
mSession.setActive(true); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public void onPause() { |
| 215 |
super.onPause(); |
| 216 |
if (mVideoView.isPlaying()) { |
| 217 |
if (!requestVisibleBehind(true)) { |
| 218 |
// Try to play behind launcher, but if it fails, stop playback. |
| 219 |
stopPlayback(); |
| 220 |
} |
| 221 |
} else { |
| 222 |
requestVisibleBehind(false); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
protected void onStop() { |
| 228 |
super.onStop(); |
| 229 |
mSession.release(); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
@Override |
| 234 |
public void onVisibleBehindCanceled() { |
| 235 |
super.onVisibleBehindCanceled(); |
| 236 |
} |
| 237 |
|
| 238 |
private void stopPlayback() { |
| 239 |
if (mVideoView != null) { |
| 240 |
mVideoView.stopPlayback(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* List of various states that we can be in |
| 246 |
*/ |
| 247 |
public enum LeanbackPlaybackState { |
| 248 |
PLAYING, PAUSED, BUFFERING, IDLE |
| 249 |
} |
| 250 |
|
| 251 |
private class MediaSessionCallback extends MediaSession.Callback { |
| 252 |
} |
| 253 |
} |