1// CHECKSTYLE:OFF Generated code
2/* This file is auto-generated from OnboardingDemoFragment.java.  DO NOT MODIFY. */
3
4/*
5 * Copyright (C) 2016 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
15 * the License.
16 */
17package com.example.android.leanback;
18
19import android.net.Uri;
20import android.os.Bundle;
21import android.support.v17.leanback.app.VideoSupportFragmentGlueHost;
22import android.support.v17.leanback.media.MediaPlayerAdapter;
23import android.support.v17.leanback.media.PlaybackGlue;
24import android.support.v17.leanback.media.PlaybackTransportControlGlue;
25import android.support.v17.leanback.widget.PlaybackControlsRow;
26
27/**
28 * Fragment demonstrating the use of {@link android.support.v17.leanback.app.VideoSupportFragment} to
29 * render video with playback controls. And demonstrates video seeking with thumbnails.
30 *
31 * Generate 1 frame per second thumbnail bitmaps and put on sdcard:
32 * <pre>
33 * sudo apt-get install libav-tools
34 * avconv -i input.mp4 -s 240x135 -vsync 1 -r 1 -an -y -qscale 8 frame_%04d.jpg
35 * adb shell mkdir /sdcard/seek
36 * adb push frame_*.jpg /sdcard/seek/
37 * </pre>
38 * Change to 1 frame per minute: use "-r 1/60".
39 * For more options, see https://wiki.libav.org/Snippets/avconv
40 *
41 * <p>
42 * Showcase:
43 * </p>
44 * <li>Auto play when ready</li>
45 * <li>Set seek provider</li>
46 * <li>switch MediaSource</li>
47 * <li>switch PlaybackGlue</li>
48 */
49public class SampleVideoSupportFragment extends android.support.v17.leanback.app.VideoSupportFragment {
50    private PlaybackTransportControlGlueSample<MediaPlayerAdapter> mMediaPlayerGlue;
51
52    final VideoSupportFragmentGlueHost mHost = new VideoSupportFragmentGlueHost(SampleVideoSupportFragment.this);
53
54    static void playWhenReady(PlaybackGlue glue) {
55        if (glue.isPrepared()) {
56            glue.play();
57        } else {
58            glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
59                @Override
60                public void onPreparedStateChanged(PlaybackGlue glue) {
61                    if (glue.isPrepared()) {
62                        glue.removePlayerCallback(this);
63                        glue.play();
64                    }
65                }
66            });
67        }
68    }
69
70    static void loadSeekData(final PlaybackTransportControlGlue glue) {
71        if (glue.isPrepared()) {
72            glue.setSeekProvider(new PlaybackSeekDiskDataProvider(
73                    glue.getDuration(),
74                    1000,
75                    "/sdcard/seek/frame_%04d.jpg"));
76        } else {
77            glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
78                @Override
79                public void onPreparedStateChanged(PlaybackGlue glue) {
80                    if (glue.isPrepared()) {
81                        glue.removePlayerCallback(this);
82                        PlaybackTransportControlGlue transportControlGlue =
83                                (PlaybackTransportControlGlue) glue;
84                        transportControlGlue.setSeekProvider(new PlaybackSeekDiskDataProvider(
85                                transportControlGlue.getDuration(),
86                                1000,
87                                "/sdcard/seek/frame_%04d.jpg"));
88                    }
89                }
90            });
91        }
92    }
93
94    @Override
95    public void onCreate(Bundle savedInstanceState) {
96        super.onCreate(savedInstanceState);
97        mMediaPlayerGlue = new PlaybackTransportControlGlueSample(getActivity(),
98                new MediaPlayerAdapter(getActivity()));
99        mMediaPlayerGlue.setHost(mHost);
100        mMediaPlayerGlue.setMode(PlaybackControlsRow.RepeatAction.INDEX_NONE);
101        mMediaPlayerGlue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
102            boolean mSecondCompleted = false;
103            @Override
104            public void onPlayCompleted(PlaybackGlue glue) {
105                if (!mSecondCompleted) {
106                    mSecondCompleted = true;
107                    mMediaPlayerGlue.setSubtitle("Leanback artist Changed!");
108                    mMediaPlayerGlue.setTitle("Leanback team at work");
109                    String uriPath = "https://storage.googleapis.com/android-tv/Sample videos/"
110                            + "April Fool's 2013/Explore Treasure Mode with Google Maps.mp4";
111                    mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(uriPath));
112                    loadSeekData(mMediaPlayerGlue);
113                    playWhenReady(mMediaPlayerGlue);
114                } else {
115                    mMediaPlayerGlue.removePlayerCallback(this);
116                    switchAnotherGlue();
117                }
118            }
119        });
120        mMediaPlayerGlue.setSubtitle("Leanback artist");
121        mMediaPlayerGlue.setTitle("Leanback team at work");
122        String uriPath = "https://storage.googleapis.com/android-tv/Sample videos/"
123                + "April Fool's 2013/Explore Treasure Mode with Google Maps.mp4";
124        mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(uriPath));
125        loadSeekData(mMediaPlayerGlue);
126        playWhenReady(mMediaPlayerGlue);
127    }
128
129    @Override
130    public void onPause() {
131        if (mMediaPlayerGlue != null) {
132            mMediaPlayerGlue.pause();
133        }
134        super.onPause();
135    }
136
137    void switchAnotherGlue() {
138        mMediaPlayerGlue = new PlaybackTransportControlGlueSample(getActivity(),
139                new MediaPlayerAdapter(getActivity()));
140        mMediaPlayerGlue.setMode(PlaybackControlsRow.RepeatAction.INDEX_ONE);
141        mMediaPlayerGlue.setSubtitle("A Googler");
142        mMediaPlayerGlue.setTitle("Swimming with the fishes");
143        mMediaPlayerGlue.getPlayerAdapter().setDataSource(
144                Uri.parse("http://techslides.com/demos/sample-videos/small.mp4"));
145        mMediaPlayerGlue.setHost(mHost);
146        loadSeekData(mMediaPlayerGlue);
147        playWhenReady(mMediaPlayerGlue);
148    }
149}
150