Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube Player plays for 2 seconds and stop in full screen

I'm currently facing 2 major problems,

  1. I'm using a youtube player and when it gets on full screen, It plays for 1-2 seconds and stop.

  2. When I click the "Play" button in the middle, it's buffering all over again. even if the gray bar filled to it's center.

those problems aren't occurring in portrait mode.

here is my class, like the youtube api demo with a bit defference

public class Video extends YouTubeFailureRecoveryActivity implements YouTubePlayer.OnFullscreenListener, Utils.OnGetUrlListener, View.OnClickListener {

    static int AUTO_PLAY_DELAY = 1000;

    static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
            ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

    private LinearLayout mRootLayout;

    /**
     * * Youtube ***
     */
    YouTubePlayerView mPlayerView;
    YouTubePlayer mPlayer;
    boolean mIsFullscreen;
    String urlID;

    /**
     * * My ***
     */
    RelativeLayout mContainer;
    ImageView mBtPlay;
    boolean mIsNeedSetFlags;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.getInstance().setActivity(this);

        setContentView(R.layout.video_main);
        mIsNeedSetFlags = true;



        mRootLayout = (LinearLayout) findViewById(R.id.video_root_layout);

       mContainer = (RelativeLayout) findViewById(R.id.container);

        mBtPlay = (ImageView) findViewById(R.id.video_play);
        mBtPlay.setVisibility(View.INVISIBLE);

        mPlayerView = (YouTubePlayerView) findViewById(R.id.player);

        Intent intent = getIntent();


        doLayout(); 
  }


    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
        mPlayer = player;

        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
        player.setOnFullscreenListener(this);

        if (mIsNeedSetFlags) {
            mIsNeedSetFlags = false;
            int controlFlags = player.getFullscreenControlFlags();
            setRequestedOrientation(PORTRAIT_ORIENTATION);
            controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            player.setFullscreenControlFlags(controlFlags);
        }

        if (!wasRestored) {
            player.cueVideo(urlID);
        }
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return mPlayerView;
    }

    private void doLayout() {
        LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) mPlayerView.getLayoutParams();
        if (mIsFullscreen) {
            playerParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            playerParams.height = LinearLayout.LayoutParams.MATCH_PARENT;

            mParallaxScrollView.setVisibility(View.GONE);
        } else {
            mParallaxScrollView.setVisibility(View.VISIBLE);

            if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                ViewGroup.LayoutParams otherViewsParams = mParallaxScrollView.getLayoutParams();
                playerParams.width = otherViewsParams.width = MATCH_PARENT;
                playerParams.height = WRAP_CONTENT;
                mRootLayout.setOrientation(LinearLayout.VERTICAL);
            }
        }
        mPlayerView.setLayoutParams(playerParams);
    }

    @Override
    public void onFullscreen(boolean isFullscreen) {
        mIsFullscreen = isFullscreen;
        showPlayerAndPlay();
        doLayout();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        doLayout();
    }

    @Override
    public void onGetUrlFinished(String videoUrl) {
        urlID = videoUrl;
        mBtPlay.setVisibility(View.VISIBLE);
        mBtPlay.setOnClickListener(this);
        mPlayerView.initialize(Utils.YOU_TUBE_DEV_KEY, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_play:
                showPlayerAndPlay();
                break;
        }
    }


    private void showPlayerAndPlay() {
        mPlayerView.setVisibility(View.VISIBLE);
        mBtPlay.setVisibility(View.INVISIBLE);

        if (!mPlayer.isPlaying())
            new android.os.Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mPlayer.play();
                }
            }, AUTO_PLAY_DELAY);
    }
}
like image 524
nirh216 Avatar asked Dec 24 '22 20:12

nirh216


2 Answers

YouTube does not allow other views to be overlayed on top of its player view.

If you check the logs, you will also see a warning message that specifies this very limitation, plus more information on which view (its ID) and the overlapping region.

like image 176
Sebastiano Avatar answered Jan 19 '23 01:01

Sebastiano


A good alternative is to used Exoplayer, to overlay your video with view. 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/ https://developer.android.com/guide/topics/media/exoplayer.html

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.

like image 38
Renaud Boulard Avatar answered Jan 19 '23 00:01

Renaud Boulard