1 /*
2  * Copyright (C) 2017 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 
17 package com.example.android.leanback;
18 
19 import android.net.Uri;
20 import android.os.Bundle;
21 
22 import androidx.leanback.app.VideoFragment;
23 import androidx.leanback.app.VideoFragmentGlueHost;
24 import androidx.leanback.media.MediaPlayerAdapter;
25 import androidx.leanback.media.PlaybackBannerControlGlue;
26 import androidx.leanback.media.PlaybackGlue;
27 
28 /**
29  * Fragment used as Control Glue's host
30  */
31 public class VideoConsumptionWithDetailCardFragment extends VideoFragment {
32 
33     public static final String TAG = "VideoConsumptionWithDetailCardFragment";
34     // A valid video URL to play video. So the progress bar can be seen to reproduce the bug.
35     private static final String VIDEO_URL =
36             "https://storage.googleapis.com/android-tv/Sample videos/"
37                     + "April Fool's 2013/Explore Treasure Mode with Google Maps.mp4";
38     public static final String TITLE = "Diving with Sharks";
39     public static final String SUBTITLE = "A Googler";
40 
41     private PlaybackBannerControlGlue<MediaPlayerAdapter> mMediaPlayerGlue;
42     final VideoFragmentGlueHost mHost = new VideoFragmentGlueHost(this);
43 
44     /**
45      * helper function for playBackGlue to add/ remove callbacks
46      *
47      * @param glue The playback glue attached to this fragment
48      */
playWhenReady(PlaybackGlue glue)49     private static void playWhenReady(PlaybackGlue glue) {
50         if (glue.isPrepared()) {
51             glue.play();
52         } else {
53             glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
54                 @Override
55                 public void onPreparedStateChanged(PlaybackGlue glue) {
56                     if (glue.isPrepared()) {
57                         glue.removePlayerCallback(this);
58                         glue.play();
59                     }
60                 }
61             });
62         }
63     }
64 
65     @Override
onCreate(Bundle savedInstanceState)66     public void onCreate(Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68 
69         int[] defuatSpeed = new int[]{1};
70         mMediaPlayerGlue = new PlaybackBannerControlGlue<>(getActivity(), defuatSpeed,
71                 new MediaPlayerAdapter(getActivity()));
72         // attach player glue to current host
73         mMediaPlayerGlue.setHost(mHost);
74 
75         // add image resource to the PlaybackControlGlue
76         mMediaPlayerGlue.setArt(getActivity().getDrawable(R.drawable.google_map));
77 
78         // meta information for video player
79         mMediaPlayerGlue.setTitle(TITLE);
80         mMediaPlayerGlue.setSubtitle(SUBTITLE);
81         mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(VIDEO_URL));
82         playWhenReady(mMediaPlayerGlue);
83         setBackgroundType(BG_LIGHT);
84     }
85 
86     @Override
onPause()87     public void onPause() {
88         if (mMediaPlayerGlue != null) {
89             mMediaPlayerGlue.pause();
90         }
91         super.onPause();
92     }
93 }
94 
95