Search code examples
androidandroid-fragmentsandroid-youtube-api

How to get YouTubeBaseActivity using YouTube API in Fragment?


I've tried to play youtube video on android with YouTubePlayerSupportFragment.

But in my case, I have to overlay play button on top of youtube player so Ive decided to use YouTubeBaseActivity.

My class is extending BaseFragment (Fragment) which means I can't extends YouTubeBaseActivity. ( can't extends 2 sigh...)

  1. Is there any method to get YouTubeBaseActivity without extending it?

  2. or can I get YouTubePlayerView like I've done with YouTubePlayerSupportFragment ( as below).

  3. If its not possible then can I display play button only on the player?? (Chromeless style doesn't show the play button)

public class StudentFragment extends BaseFragment{

...

public View onCreateView{

fragment1 = (YouTubeFragment) getChildFragmentManager().findFragmentById(R.id.fragment_youtube);

fragment1.setVideoId(movie_Id);

}




public class YouTubeFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener{

public void setVideoId(final String videoId) {

mVideoId = videoId;

initialize(youtube_developer_key, this);

}

etc...


Solution

  • I've tried to play youtube video on android with YouTubePlayerSupportFragment.

    But in my case, I have to overlay play button on top of youtube player so Ive decided to use YouTubeBaseActivity.

    It does not make any difference. You will experience the same behavior using the Activity and/or the Fragment.

    My class is extending BaseFragment (Fragment) which means I can't extends YouTubeBaseActivity. ( can't extends 2 sigh...)

    Even if it was possible, it would not make any sense to have a single class that extends both an Activity and a Fragment.

    Is there any method to get YouTubeBaseActivity without extending it?

    or can I get YouTubePlayerView like I've done with YouTubePlayerSupportFragment ( as below).

    You have two simple options: either use the YouTubeBaseActivity + YouTubePlayerView, or go with the YouTubeSupportFragment.

    If its not possible then can I display play button only on the player?? (Chromeless style doesn't show the play button)

    And now we get to the point: you cannot place any View over the YouTube player, otherwise video playback will keep stopping every second or so.

    You can try using the STANDARD style for the YouTubePlayer instance (which, if I am not mistaken, already shows a button over the video) and see if that suits your needs.