7

I am using an Youtube Player api for playing youtube videos in my application. Video start playing and pausing after 1-2 seconds

I created Video Fragment and ViewGroup. Subsequently I create some youtobe videoview.

VideoFragment

public static final class VideoFragment extends YouTubePlayerSupportFragment implements
        OnInitializedListener
{

    private YouTubePlayer player;
    private String videoId;

    public static VideoFragment newInstance()
    {
        return new VideoFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        initialize(Constants.DEVELOPER_KEY, this);
    }

    @Override
    public void onDestroy()
    {
        if (player != null)
        {
            player.release();
        }
        super.onDestroy();
    }

    public void setVideoId(String videoId)
    {
        if (videoId != null && !videoId.equals(this.videoId))
        {
            this.videoId = videoId;
            if (player != null)
            {
                player.cueVideo(videoId);
            }
        }
    }

    public void pause()
    {
        if (player != null)
        {
            player.pause();
        }
    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player,
            boolean restored)
    {
        this.player = player;
        if (!restored && videoId != null)
        {
            player.cueVideo(videoId);
        }
    }

    @Override
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result)
    {
        this.player = null;
    }

}

Function for creating Youtobe videoview

private ViewGroup createYouTubePlayer(final VideoData data, final FrameLayout youTubePlayer)
{

    youTubePlayer.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            FragmentManager fm = mActivity.getSupportFragmentManager();

            if (v.getId() == mCurrentYouTubePlayer)
            {
                return;
            }

            VideoFragment fragment = (VideoFragment) fm.findFragmentById(mCurrentYouTubePlayer);
            if (fragment == null)
            {
                fragment = VideoFragment.newInstance();
                fragment.setVideoId(data.srcPath);

                fm.beginTransaction().add(youTubePlayer.getId(), fragment).commit();
                mCurrentYouTubePlayer = v.getId();
            }
            else
            {
                fm.beginTransaction().remove(fragment).commit();

                fragment = VideoFragment.newInstance();
                fragment.setVideoId(data.srcPath);
                fm.beginTransaction().add(youTubePlayer.getId(), fragment).commit();
                mCurrentYouTubePlayer = v.getId();
            }
        }
    });



    return youTubePlayer;
}
user3524002
  • 71
  • 1
  • 2

3 Answers3

9

It is not possible to add buttons as overlays above the player as specified by Google, otherwise the player will stop:

https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerView

Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay the view with other views while a video is playing.

This view does not support padding. To achieve the same effect, wrap the view in another ViewGroup or give it margins.

Padding is also not supported on YouTubePlayer.

To overlay your view on video, I will recommend you to use ExoPlayer, It is not part of the android sdk, but it's recommended by google and included in android developer documentation :

http://google.github.io/ExoPlayer/

Exoplayer allow you to stream any kind of video, not only Youtubes videos.

It's also good to mention that Exoplayer is used in youtube application.

Renaud Boulard
  • 709
  • 6
  • 9
0

The problem may be because there is some object above com.google.android.youtube.player.YouTubePlayerView or the video has padding property

ecrem
  • 1
0

if anybody using the Youtube Fragment then use replace in transaction rather than add.

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

    transaction.replace(R.id.youtube_frame, youTubePlayerFragment).commit();
Arun Prajapati
  • 283
  • 2
  • 6