MediaControlIntent.java revision 3d4c9459ed77f732dd3ba602713af6ebf9280c8c
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v7.media; 18 19import android.app.PendingIntent; 20import android.content.Intent; 21import android.net.Uri; 22 23/** 24 * Constants for identifying media route capabilities and controlling media routes 25 * by sending an {@link Intent}. 26 * <p> 27 * The basic capabilities of a media route may be determined by looking at the 28 * media control intent categories and actions supported by the route. 29 * </p><ul> 30 * <li>A media control intent category specifies the type of the route 31 * and the manner in which applications send media to its destination. 32 * <li>A media control intent action specifies a command to be delivered to 33 * the media route's destination to control media playback. Media control 34 * actions may only apply to routes that support certain media control categories. 35 * </ul> 36 * 37 * <h3>Route Categories</h3> 38 * <p> 39 * Routes are classified by the categories of actions that they support. The following 40 * standard categories are defined. 41 * </p><ul> 42 * <li>{@link #CATEGORY_LIVE_AUDIO Live audio}: The route supports streaming live audio 43 * from the device to the destination. Live audio routes include local speakers 44 * and Bluetooth headsets. 45 * <li>{@link #CATEGORY_LIVE_VIDEO Live video}: The route supports streaming live video 46 * from the device to the destination. Live video routes include local displays 47 * and wireless displays that support mirroring and 48 * {@link android.app.Presentation presentations}. 49 * <li>{@link #CATEGORY_REMOTE_PLAYBACK Remote playback}: The route supports sending 50 * remote playback requests for media content to the destination. The content to be 51 * played is identified by a Uri and mime-type. 52 * </ul> 53 * 54 * <h3>Remote Playback</h3> 55 * <p> 56 * Media control intents are frequently used to start remote playback of media 57 * on a destination using remote playback actions from the 58 * {@link #CATEGORY_REMOTE_PLAYBACK remote playback category}. 59 * </p><p> 60 * The {@link #ACTION_PLAY} action enqueues the Uri of content to be played and obtains 61 * a media item id that can be used to control playback. 62 * </p> 63 * 64 * <h4>Media Items</h4> 65 * <p> 66 * A media item id is an opaque token that represents the playback request. 67 * The application must supply the media item id when sending control requests to 68 * {@link #ACTION_PAUSE pause}, {@link #ACTION_RESUME resume}, {@link #ACTION_SEEK seek}, 69 * {@link #ACTION_GET_STATUS get status}, or perform other actions to affect playback. 70 * </p><p> 71 * Each remote playback action is bound to a specific media item. If a 72 * media item has finished, been canceled or encountered an error, then most 73 * actions other than status requests will fail. In particular, actions such as 74 * {@link #ACTION_PAUSE} always control playback of a specific media item rather 75 * than acting globally upon whatever happens to be playing at the moment. 76 * </p> 77 * 78 * <h4>Queue Behavior</h4> 79 * <p> 80 * To provide a seamless media experience, the application can enqueue a limited number 81 * of items to play in succession. The destination can take advantage of its 82 * queue to optimize continuous playback, starting the next media item automatically 83 * as soon as the previous one finishes. 84 * </p><p> 85 * By default, the {@link #ACTION_PLAY play action} causes the destination to stop 86 * whatever is currently playing, clear the queue of pending items, then begin playing 87 * the newly requested content. By supplying a 88 * {@link #EXTRA_ITEM_QUEUE_BEHAVIOR queue behavior} parameter as part of the playback 89 * request, the application can specify whether the media item should 90 * {@link #ITEM_QUEUE_BEHAVIOR_PLAY_NOW play now}, 91 * {@link #ITEM_QUEUE_BEHAVIOR_PLAY_NOW play next}, 92 * or {@link #ITEM_QUEUE_BEHAVIOR_PLAY_NOW play later}, 93 * </p><p> 94 * Typically the application will start by queuing two media items at once: one item to 95 * play now and one item to play later. When the first item finishes, the next item 96 * will begin playing immediately. The application can then enqueue another media item to 97 * play next (without interrupting current playback), and so on. 98 * </p><p> 99 * The application can also enqueue additional items to play later although queuing 100 * just two items at a time is usually sufficient for continuous playback. 101 * </p> 102 */ 103public final class MediaControlIntent { 104 /** 105 * Media control category: Live audio. 106 * <p> 107 * A route that supports live audio routing will allow the media audio stream 108 * to be sent to supported destinations. This can include internal speakers or 109 * audio jacks on the device itself, A2DP devices, and more. 110 * </p><p> 111 * When a live audio route is selected, audio routing is transparent to the application. 112 * All audio played on the media stream will be routed to the selected destination. 113 * </p> 114 */ 115 public static final String CATEGORY_LIVE_AUDIO = "android.media.intent.category.LIVE_AUDIO"; 116 117 /** 118 * Media control category: Live video. 119 * <p> 120 * A route that supports live video routing will allow a mirrored version 121 * of the device's primary display or a customized 122 * {@link android.app.Presentation Presentation} to be sent to supported 123 * destinations. 124 * </p><p> 125 * When a live video route is selected, audio and video routing is transparent 126 * to the application. By default, audio and video is routed to the selected 127 * destination. For certain live video routes, the application may also use a 128 * {@link android.app.Presentation Presentation} to replace the mirrored view 129 * on the external display with different content. 130 * </p> 131 * 132 * @see MediaRouter.RouteInfo#getPresentationDisplay() 133 * @see android.app.Presentation 134 */ 135 public static final String CATEGORY_LIVE_VIDEO = "android.media.intent.category.LIVE_VIDEO"; 136 137 /** 138 * Media control category: Remote playback. 139 * <p> 140 * A route that supports remote playback routing will allow an application to send 141 * requests to play content remotely to supported destinations. 142 * </p><p> 143 * Remote playback routes destinations operate independently of the local device. 144 * When a remote playback route is selected, the application can control the content 145 * playing on the destination by sending media control actions to the route. 146 * The application may also receive status updates from the route regarding 147 * remote playback. 148 * </p> 149 * 150 * @see MediaRouter.RouteInfo#sendControlRequest 151 */ 152 public static final String CATEGORY_REMOTE_PLAYBACK = 153 "android.media.intent.category.REMOTE_PLAYBACK"; 154 155 /** 156 * Media control action: Play media item. 157 * <p> 158 * Used with routes that support {@link #CATEGORY_REMOTE_PLAYBACK remote playback} 159 * media control. 160 * </p><p> 161 * This action causes a remote playback route to start playing content with 162 * the {@link Uri} specified in the {@link Intent}'s {@link Intent#getData() data uri}. 163 * The action returns a media item id which can be used to control playback 164 * using other remote playback actions. 165 * </p><p> 166 * Once initiated, playback of the specified content will be queued and managed 167 * independently by the destination. The application will receive status 168 * and progress updates as the content is played. 169 * </p><p> 170 * If the data uri specifies an HTTP or HTTPS scheme, then the destination is 171 * responsible for following HTTP redirects to a reasonable depth of at least 3 172 * levels as might typically be handled by a web browser. If an HTTP error 173 * occurs, then the destination should send a {@link MediaItemStatus status update} 174 * back to the client indicating the {@link MediaItemStatus#PLAYBACK_STATE_ERROR error} 175 * {@link MediaItemStatus#getPlaybackState() playback state} 176 * and include the {@link MediaItemStatus#getHttpStatusCode() HTTP status code}. 177 * </p> 178 * 179 * <h3>Request parameters</h3> 180 * <ul> 181 * <li>{@link #EXTRA_ITEM_QUEUE_BEHAVIOR}: specifies when the content should be played. 182 * <li>{@link #EXTRA_ITEM_CONTENT_POSITION}: specifies the initial content playback position. 183 * <li>{@link #EXTRA_ITEM_METADATA}: specifies metadata associated with the 184 * content such as the title of a song. 185 * <li>{@link #EXTRA_ITEM_STATUS_UPDATE_RECEIVER}: specifies a {@link PendingIntent} 186 * for a broadcast receiver that will receive status updates about the media item. 187 * <li>{@link #EXTRA_ITEM_HTTP_HEADERS}: specifies HTTP headers to supply to the 188 * server when fetching the content. 189 * </ul> 190 * 191 * <h3>Result data</h3> 192 * <ul> 193 * <li>{@link #EXTRA_ITEM_ID}: specifies an opaque string identifier to use to refer 194 * to the media item in subsequent requests such as {@link #ACTION_PAUSE}. 195 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the initial status of the item 196 * that has been enqueued. 197 * </ul> 198 * 199 * <h3>Status updates</h3> 200 * <p> 201 * If the client supplies a {@link #EXTRA_ITEM_STATUS_UPDATE_RECEIVER status update receiver} 202 * then the media route provider is responsible for sending status updates to the receiver 203 * when significant media item state changes occur such as when playback starts or 204 * stops. The receiver will not be invoked for content playback position changes. 205 * The application may retrieve the current playback position when necessary 206 * using the {@link #ACTION_GET_STATUS} request. 207 * </p><p> 208 * Refer to {@link MediaItemStatus} for details. 209 * </p> 210 * 211 * <h3>Example</h3> 212 * <pre> 213 * MediaRouter mediaRouter = MediaRouter.getInstance(context); 214 * MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(); 215 * Intent intent = new Intent(MediaControlIntent.ACTION_PLAY); 216 * intent.addCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK); 217 * intent.setDataAndType("http://example.com/videos/movie.mp4", "video/mp4"); 218 * intent.putExtra(MediaControlIntent.EXTRA_ITEM_QUEUE_BEHAVIOR, 219 * MediaControlIntent.ITEM_QUEUE_BEHAVIOR_PLAY_NEXT); 220 * if (route.supportsControlRequest(intent)) { 221 * MediaRouter.ControlRequestCallback callback = new MediaRouter.ControlRequestCallback() { 222 * public void onResult(int result, Bundle data) { 223 * if (result == REQUEST_SUCCEEDED) { 224 * // The request succeeded. 225 * // Playback may be controlled using the returned item id... 226 * String id = data.getStringExtra(MediaControlIntent.EXTRA_ITEM_ID); 227 * } 228 * } 229 * }; 230 * route.sendControlRequest(intent, callback); 231 * }</pre> 232 * 233 * @see MediaRouter.RouteInfo#sendControlRequest 234 * @see #CATEGORY_REMOTE_PLAYBACK 235 * @see #ACTION_SEEK 236 * @see #ACTION_STOP 237 * @see #ACTION_PAUSE 238 * @see #ACTION_RESUME 239 * @see #ACTION_GET_STATUS 240 */ 241 public static final String ACTION_PLAY = "android.media.intent.action.PLAY"; 242 243 /** 244 * Media control action: Seek media item to a new playback position. 245 * <p> 246 * Used with routes that support {@link #CATEGORY_REMOTE_PLAYBACK remote playback} 247 * media control. 248 * </p><p> 249 * This action causes a remote playback route to modify the current playback 250 * position of the specified media item. 251 * </p><p> 252 * This action should generally not affect the current playback state of the media item. 253 * If the item is paused, then seeking should set the position but leave 254 * the item paused. Likewise if the item is playing, then seeking should 255 * continue playing from the new position. If the item has not yet started 256 * playing, then the new playback position should be remembered and used as the 257 * initial position for the item. 258 * </p> 259 * 260 * <h3>Request parameters</h3> 261 * <ul> 262 * <li>{@link #EXTRA_ITEM_ID}: specifies the media item id of the playback to be 263 * controlled. This value was returned as a result from the 264 * {@link #ACTION_PLAY play} action. 265 * <li>{@link #EXTRA_ITEM_CONTENT_POSITION}: specifies the new position of the content. 266 * </ul> 267 * 268 * <h3>Result data</h3> 269 * <ul> 270 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the status of the stream. 271 * </ul> 272 * 273 * @see MediaRouter.RouteInfo#sendControlRequest 274 * @see #CATEGORY_REMOTE_PLAYBACK 275 */ 276 public static final String ACTION_SEEK = "android.media.intent.action.SEEK"; 277 278 /** 279 * Media control action: Stop media item playback. 280 * <p> 281 * Used with routes that support {@link #CATEGORY_REMOTE_PLAYBACK remote playback} 282 * media control. 283 * </p><p> 284 * This action causes a remote playback route to stop playback of the 285 * specified media item. 286 * </p><p> 287 * If the media item has not started playing yet, then the media item should 288 * be stopped and removed from the queue. 289 * </p> 290 * 291 * <h3>Request parameters</h3> 292 * <ul> 293 * <li>{@link #EXTRA_ITEM_ID}: specifies the media item id of the playback to be 294 * controlled. This value was returned as a result from the 295 * {@link #ACTION_PLAY play} action. 296 * </ul> 297 * 298 * <h3>Result data</h3> 299 * <ul> 300 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the status of the stream. 301 * </ul> 302 * 303 * @see MediaRouter.RouteInfo#sendControlRequest 304 * @see #CATEGORY_REMOTE_PLAYBACK 305 */ 306 public static final String ACTION_STOP = "android.media.intent.action.STOP"; 307 308 309 /** 310 * Media control action: Pause media item playback. 311 * <p> 312 * Used with routes that support {@link #CATEGORY_REMOTE_PLAYBACK remote playback} 313 * media control. 314 * </p><p> 315 * This action causes a remote playback route to pause playback of the 316 * specified media item. 317 * </p><p> 318 * If the media item has not started playing yet, then the request to pause should 319 * be remembered such that the item will initially start in a paused state. 320 * </p> 321 * 322 * <h3>Request parameters</h3> 323 * <ul> 324 * <li>{@link #EXTRA_ITEM_ID}: specifies the media item id of the playback to be 325 * controlled. This value was returned as a result from the 326 * {@link #ACTION_PLAY play} action. 327 * </ul> 328 * 329 * <h3>Result data</h3> 330 * <ul> 331 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the status of the stream. 332 * </ul> 333 * 334 * @see MediaRouter.RouteInfo#sendControlRequest 335 * @see #CATEGORY_REMOTE_PLAYBACK 336 * @see #ACTION_RESUME 337 */ 338 public static final String ACTION_PAUSE = "android.media.intent.action.PAUSE"; 339 340 /** 341 * Media control action: Resume media item playback (unpause). 342 * <p> 343 * Used with routes that support {@link #CATEGORY_REMOTE_PLAYBACK remote playback} 344 * media control. 345 * </p><p> 346 * This action causes a remote playback route to resume playback of the 347 * specified media item. Reverses the effects of {@link #ACTION_PAUSE}. 348 * </p> 349 * 350 * <h3>Request parameters</h3> 351 * <ul> 352 * <li>{@link #EXTRA_ITEM_ID}: specifies the media item id of the playback to be 353 * controlled. This value was returned as a result from the 354 * {@link #ACTION_PLAY play} action. 355 * </ul> 356 * 357 * <h3>Result data</h3> 358 * <ul> 359 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the status of the stream. 360 * </ul> 361 * 362 * @see MediaRouter.RouteInfo#sendControlRequest 363 * @see #CATEGORY_REMOTE_PLAYBACK 364 * @see #ACTION_PAUSE 365 */ 366 public static final String ACTION_RESUME = "android.media.intent.action.RESUME"; 367 368 /** 369 * Media control action: Get media item playback status and progress information. 370 * <p> 371 * Used with routes that support {@link #CATEGORY_REMOTE_PLAYBACK remote playback} 372 * media control. 373 * </p><p> 374 * This action asks a remote playback route to provide updated playback status and progress 375 * information about the specified media item. 376 * </p> 377 * 378 * <h3>Request parameters</h3> 379 * <ul> 380 * <li>{@link #EXTRA_ITEM_ID}: specifies the media item id of the playback to be 381 * controlled. This value was returned as a result from the 382 * {@link #ACTION_PLAY play} action. 383 * </ul> 384 * 385 * <h3>Result data</h3> 386 * <ul> 387 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the status of the stream. 388 * </ul> 389 * 390 * @see MediaRouter.RouteInfo#sendControlRequest 391 * @see #CATEGORY_REMOTE_PLAYBACK 392 * @see #EXTRA_ITEM_STATUS_UPDATE_RECEIVER 393 */ 394 public static final String ACTION_GET_STATUS = "android.media.intent.action.GET_STATUS"; 395 396 /** 397 * Integer extra: Media item queue behavior. 398 * <p> 399 * Used with {@link #ACTION_PLAY} to specify when the requested should be 400 * played. The default is to play the content immediately. 401 * </p><p> 402 * The value must be one of {@link #ITEM_QUEUE_BEHAVIOR_PLAY_NOW}, 403 * {@link #ITEM_QUEUE_BEHAVIOR_PLAY_NEXT}, or {@link #ITEM_QUEUE_BEHAVIOR_PLAY_LATER}. 404 * </p> 405 * 406 * @see #ACTION_PLAY 407 */ 408 public static final String EXTRA_ITEM_QUEUE_BEHAVIOR = 409 "android.media.intent.extra.QUEUE_BEHAVIOR"; 410 411 /** 412 * Value for {@link #EXTRA_ITEM_QUEUE_BEHAVIOR}: Play now. 413 * <p> 414 * This is the default queue behavior. 415 * </p><p> 416 * Requests that the new content be played immediately, canceling the currently playing 417 * media item and all subsequent items in the queue. When this control request returns, 418 * the queue will contain exactly one item consisting of the newly requested content. 419 * </p> 420 * 421 * @see #EXTRA_ITEM_QUEUE_BEHAVIOR 422 */ 423 public static final int ITEM_QUEUE_BEHAVIOR_PLAY_NOW = 0; 424 425 /** 426 * Value for {@link #EXTRA_ITEM_QUEUE_BEHAVIOR}: Play next. 427 * <p> 428 * Requests that the new content be enqueued to play next after the currently playing 429 * media item, canceling all subsequent items in the queue. When this control request 430 * returns, the queue will contain either one or two items consisting of the currently 431 * playing content, if any, followed by the newly requested content. 432 * </p> 433 * 434 * @see #EXTRA_ITEM_QUEUE_BEHAVIOR 435 */ 436 public static final int ITEM_QUEUE_BEHAVIOR_PLAY_NEXT = 1; 437 438 /** 439 * Value for {@link #EXTRA_ITEM_QUEUE_BEHAVIOR}: Play later. 440 * <p> 441 * Requests that the new content be enqueued to play later after all other media items 442 * currently in the queue. When this control request returns, the queue will contain at 443 * least one item consisting of the currently playing content and all previously 444 * enqueued items followed by the newly requested content. 445 * </p> 446 * 447 * @see #EXTRA_ITEM_QUEUE_BEHAVIOR 448 */ 449 public static final int ITEM_QUEUE_BEHAVIOR_PLAY_LATER = 2; 450 451 /** 452 * Double extra: Media item content position. 453 * <p> 454 * Used with {@link #ACTION_PLAY} to specify the starting playback position. 455 * </p><p> 456 * Used with {@link #ACTION_SEEK} to set a new playback position. 457 * </p><p> 458 * The value is a double-precision floating point number of seconds 459 * from the beginning of the content. 460 * <p> 461 * 462 * @see #ACTION_PLAY 463 * @see #ACTION_SEEK 464 */ 465 public static final String EXTRA_ITEM_CONTENT_POSITION = 466 "android.media.intent.extra.ITEM_POSITION"; 467 468 /** 469 * Bundle extra: Media item metadata. 470 * <p> 471 * Used with {@link #ACTION_PLAY} to specify metadata associated with the content 472 * of a media item. 473 * </p><p> 474 * The value is a {@link android.os.Bundle} of metadata keys and values as defined 475 * in {@link MediaItemMetadata}. 476 * </p> 477 * 478 * @see #ACTION_PLAY 479 */ 480 public static final String EXTRA_ITEM_METADATA = 481 "android.media.intent.extra.ITEM_METADATA"; 482 483 /** 484 * Bundle extra: Media item id. 485 * <p> 486 * Returned as a result from {@link #ACTION_PLAY} to provide an opaque unique id 487 * for the requested media item which may then be used to issue subsequent 488 * requests to control the content. 489 * </p><p> 490 * Used with various actions to specify the id of the media item to be controlled. 491 * </p><p> 492 * The value is a unique string value generated by the media route provider 493 * to represent one particular media item. 494 * </p> 495 * 496 * @see #ACTION_PLAY 497 * @see #ACTION_STOP 498 * @see #ACTION_SEEK 499 * @see #ACTION_PAUSE 500 * @see #ACTION_RESUME 501 * @see #ACTION_GET_STATUS 502 */ 503 public static final String EXTRA_ITEM_ID = 504 "android.media.intent.extra.ITEM_ID"; 505 506 /** 507 * Bundle extra: Media item status. 508 * <p> 509 * Returned as a result from {@link #ACTION_GET_STATUS} and in broadcasts 510 * sent to a {@link #EXTRA_ITEM_STATUS_UPDATE_RECEIVER status update receiver} 511 * to describe the status of the media item. 512 * </p><p> 513 * The value is a {@link android.os.Bundle} of status keys and values as defined 514 * in {@link MediaItemStatus}. 515 * </p> 516 * 517 * @see #ACTION_PLAY 518 * @see #ACTION_GET_STATUS 519 */ 520 public static final String EXTRA_ITEM_STATUS = 521 "android.media.intent.extra.ITEM_STATUS"; 522 523 /** 524 * Bundle extra: Media item status update receiver. 525 * <p> 526 * Used with {@link #ACTION_PLAY} to specify a {@link PendingIntent} for a 527 * broadcast receiver that will receive status updates about a media item. 528 * </p><p> 529 * Whenever the status of the media item changes, the media route provider will 530 * send a broadcast to the pending intent with extras that describe 531 * the status of the media item. 532 * </p><p> 533 * The value is a {@link PendingIntent}. 534 * </p> 535 * 536 * <h3>Broadcast extras</h3> 537 * <ul> 538 * <li>{@link #EXTRA_ITEM_ID}: specifies the media item id of the playback to be 539 * controlled. This value was returned as a result from the 540 * {@link #ACTION_PLAY play} action. 541 * <li>{@link #EXTRA_ITEM_STATUS}: specifies the status of the stream. 542 * </ul> 543 * 544 * @see #ACTION_PLAY 545 */ 546 public static final String EXTRA_ITEM_STATUS_UPDATE_RECEIVER = 547 "android.media.intent.extra.ITEM_STATUS_UPDATE_RECEIVER"; 548 549 /** 550 * Bundle extra: HTTP headers. 551 * <p> 552 * Used with {@link #ACTION_PLAY} to specify HTTP headers to be included when 553 * fetching to the content indicated by the media item's data Uri. 554 * </p><p> 555 * This extra may be used to provide authentication tokens and other 556 * parameters to the server separately from the media item's data Uri. 557 * </p><p> 558 * The value is a {@link android.os.Bundle} of string based key value pairs 559 * that describe the HTTP headers. 560 * </p> 561 * 562 * @see #ACTION_PLAY 563 */ 564 public static final String EXTRA_ITEM_HTTP_HEADERS = 565 "android.media.intent.extra.HTTP_HEADERS"; 566 567 private MediaControlIntent() { 568 } 569} 570