コンテンツに移動

よくアクセスするページ

最近アクセスしたページ

navigation

Use Leanback Transport Controls

The Leanback library has new playback controls that provide an improved user experience. For video apps, the transport controls suppport video scrubbing with the forward/backward controls. While scrubbing the display shows thumbnails to help navigate through the video.

The library includes abstract classes as well as out-of-the-box implementations that provide more granular control for developers. Using the pre-built implementations you can quickly build a feature-rich app without much coding. If you need more customization, you can extend any of the library's pre-built components.

Controls and player

The leanback library separates the UI with the transport controls from the player that plays back video. This is accomplished with two components: a playback fragment for displaying the transport controls (and optionally the video) and a player adapter to encapsulate a media player.

Playback fragment

Your app's UI Activity should use a PlaybackFragment or a VideoFragment. Both contain the leanback transport controls:

You can customize a fragment's ObjectAdapter to enhance the UI. For example, use setAdapter() to add a "related videos" row.

PlayerAdapter

PlayerAdapter is an abstract class that controls the underlying media player. Developers can chose the pre-built MediaPlayerAdapter implementation, or write their own implementation of this class.

Glueing the pieces together

You must use some "control glue" to connect the playback fragment to the player. The leanback library provides two kinds of glue:

leanback transport control glue

If you want your app to support video scrubbing you must use PlaybackTransportControlGlue.

You also need to specify a "glue host" that binds the glue to the playback fragment, draws the transport controls in the UI and maintains their state, and passes transport control events back to the glue. The host must match the playback fragment type. Use PlaybackFragmentGlueHost with a PlaybackFragment, and VideoFragmentGlueHost with a VideoFragment.

Here's an illustration showing how the pieces of a leanback transport control fit together:

leanback transport control glue

The code that glues your app together should be inside the PlaybackFragment or VideoFragment that defines the UI.

In the following example, the app constructs an instance ofPlaybackTransportControlGlue, naming it playerGlue, and connects its VideoFragment to a newly-created MediaPlayerAdapter. Since this is a VideoFragment the setup code calls setHost() to attach a VideoFragmentGlueHost to playerGlue. The code is included inside the class that extends the VideoFragment.

public class MyVideoFragment extends VideoFragment {

      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          final PlaybackTransportControlGlue<MediaPlayerAdapter> playerGlue =
                  new PlaybackTransportControlGlue(getActivity(),
                          new MediaPlayerAdapter(getActivity()));
          playerGlue.setHost(new VideoFragmentGlueHost(this));
          playerGlue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
              @Override
              public void onPreparedStateChanged(PlaybackGlue glue) {
                  if (glue.isPrepared()) {
                      playerGlue.setSeekProvider(new MySeekProvider());
                      playerGlue.play();
                  }
              }
          });
          playerGlue.setSubtitle("Leanback artist");
          playerGlue.setTitle("Leanback team at work");
          String uriPath = "android.resource://com.example.android.leanback/raw/video";
          playerGlue.getPlayerAdapter().setDataSource(Uri.parse(uriPath));
      }
  }

Note that the setup code also defines a PlayerAdapter.Callback to handle events from the media player.

Customizing the UI glue

You can customize the PlaybackBannerControlGlue and PlaybackTrabsportControlGlue. to change the PlaybackControlsRow.

Customizing the title and description

To customize the title and description at the top of the playback controls, override onCreateRowPresenter():

@Override
protected PlaybackRowPresenter onCreateRowPresenter() {
  PlaybackTransportRowPresenter presenter = (PlaybackTransportRowPresenter) super.onCreateRowPresenter();
  presenter.setDescriptionPresenter(new MyCustomDescriptionPresenter());
  return presenter;
}

Adding controls

The control glue displays controls for actions in a PlaybackControlsRow.

The actions in the PlaybackControlsRow are assigned to two groups: primary actions and secondary actions. Controls for the primary group appear above the seek bar and controls for the secondary group appear below the seek bar. Initially, there is only a single primary action for the play/pause button, and no secondary actions.

You can add actions the primary and secondary groups by overriding onCreatePrimaryActions() and onCreateSecondaryActions().

private PlaybackControlsRow.RepeatAction mRepeatAction;
    private PlaybackControlsRow.PictureInPictureAction mPipAction;
    private PlaybackControlsRow.ThumbsUpAction mThumbsUpAction;
    private PlaybackControlsRow.ThumbsDownAction mThumbsDownAction;
    private PlaybackControlsRow.SkipPreviousAction mSkipPreviousAction;
    private PlaybackControlsRow.SkipNextAction mSkipNextAction;
    private PlaybackControlsRow.FastForwardAction mFastForwardAction;
    private PlaybackControlsRow.RewindAction mRewindAction;

@Override
protected void onCreatePrimaryActions(ArrayObjectAdapter primaryActionsAdapter) {
    // Order matters, super.onCreatePrimaryActions() will create the play / pause action.
    // Will display as follows:
    // play/pause, previous, rewind, fast forward, next
    //   > /||      |<        <<        >>         >|
    super.onCreatePrimaryActions(primaryActionsAdapter);
    primaryActionsAdapter.add(mSkipPreviousAction);
    primaryActionsAdapter.add(mRewindAction);
    primaryActionsAdapter.add(mFastForwardAction);
    primaryActionsAdapter.add(mSkipNextAction);
}

@Override
protected void onCreateSecondaryActions(ArrayObjectAdapter adapter) {
    super.onCreateSecondaryActions(adapter);
    adapter.add(mThumbsDownAction);
    adapter.add(mThumbsUpAction);
}

You must override onActionClicked() to handle the new actions.

@Override
public void onActionClicked(Action action) {
    if (action == mRewindAction) {
        // Handle Rewind
    } else if (action == mFastForwardAction ) {
        // Handle FastForward
    } else if (action == mThumbsDownAction) {
        // Handle ThumbsDown
    } else if (action == mThumbsUpAction) {
        // Handle ThumbsUp
    } else {
        // The superclass handles play/pause and delegates next/previous actions to abstract methods,
        // so those two methods should be overridden rather than handling the actions here.
        super.onActionClicked(action);
    }
}

@Override
public void next() {
    // Skip to next item in playlist.
}

@Override
public void previous() {
    // Skip to previous item in playlist.
}

In special cases, you might want to implement your own PlaybackTransportRowPresenter to render custom controls and respond to seek actions using the PlaybackSeekUi.

Scrubbing video

If your app uses a VideoFragment and you want to support video scrubbing.

scrubbing

You need to provide an implementation of PlaybackSeekDataProvider. This component provides the video thumbnails used when scrolling. You must implement your own provider by extending PlaybackSeekDataProvider. See the example in the leanback-showcase app.

このサイトでは、ユーザーが選択したサイトの言語と表示設定を保存する目的で Cookie を使用しています。

Android デベロッパー向けの最新情報やヒントを入手して、Google Play での成功を手に入れましょう。

* 必須

送信しました

WeChat で Google Developers をフォローする

このサイトをで表示しますか?

ページの表示言語としてを選択しましたが、このサイトの言語はに設定されています。

言語設定を変更してこのサイトをで表示しますか?言語設定を変更する場合は、各ページの下にある言語メニューを使用してください。

このクラスには、API レベル 以上が必要です。

API レベル が選択されているため、このドキュメントは非表示になっています。左のナビゲーションの上にあるセレクタを使って、ドキュメントの API レベルを変更できます。

アプリに必要な API レベルを指定する方法について、詳しくは異なるプラットフォーム バージョンのサポートをご覧ください。

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)