在图片模式下进入或退出图片后,YouTube视频停止播放

时间:2019-06-11 08:09:06

标签: android picture-in-picture youtubeplayer

我正在尝试以画中画模式播放youtube视频。 为此,我使用的是youtube player api,当我按“后退”按钮时,youtube播放器活动以图片模式进入图片,但视频停止播放,当我按“播放”按钮时,它又再次进入全屏模式并从头开始播放。

旋转屏幕视频正常播放后在全屏模式下播放,并在旋转之前在该位置开始播放视频。

我无法继续在PIP中播放视频。

这是我的代码

public class YoutubePIPActivity extends YouTubeBaseActivity implements 
    YouTubePlayer.OnInitializedListener
    {

    private static final String API_KEY = AppConstant.YUTUBE_PLAYER_API_KEY;
    private static String mVIDEO_ID = "o7VVHhK9zf0";
    private boolean isPIPModeEnabled = true; 

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtube_pip);

        YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
        youTubePlayerView.initialize(API_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
    {
        /** add listeners to YouTubePlayer instance **/
        player.setPlayerStateChangeListener(playerStateChangeListener);
        player.setPlaybackEventListener(playbackEventListener);

        /** Start buffering **/
        if (!wasRestored) {
            player.cueVideo(mVIDEO_ID);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult)
    {
        Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
    }

    private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
        @Override
        public void onBuffering(boolean arg0) {
        }
        @Override
        public void onPaused() {
        }
        @Override
        public void onPlaying() {
        }
        @Override
        public void onSeekTo(int arg0) {
        }
        @Override
        public void onStopped() {
        }
    };

    private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
        @Override
        public void onAdStarted() {
        }
        @Override
        public void onError(YouTubePlayer.ErrorReason arg0) {
        }
        @Override
        public void onLoaded(String arg0) {
        }
        @Override
        public void onLoading() {
        }
        @Override
        public void onVideoEnded() {
        }
        @Override
        public void onVideoStarted() {
        }
    };


    @Override
    public void onBackPressed()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                && getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
                && isPIPModeEnabled)
        {
            enterPIPMode();
        }
        else
        {
            super.onBackPressed();
        }
    }

    private void enterPIPMode()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                && getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE))
        {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            {
                Rational rational = new Rational(250, 150);
                PictureInPictureParams.Builder params = new PictureInPictureParams.Builder();
                params.setAspectRatio(rational);
                this.enterPictureInPictureMode(params.build());
            } else
            {
                this.enterPictureInPictureMode();
            }

            new Handler().postDelayed(() -> checkPIPPermission(), 30);
        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    void checkPIPPermission()
    {
        isPIPModeEnabled = isInPictureInPictureMode();
        if(!isInPictureInPictureMode())
        {
            onBackPressed();
        }
    }


    @Override
    protected void onUserLeaveHint()
    {
        super.onUserLeaveHint();
        enterPIPMode();
    }

    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig)
    {
        if(newConfig !=null){
            super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        }

    }
}

这是清单中定义的活动

<activity android:name=".ui.activity.youtube.YoutubePIPActivity"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:exported="false"
            android:resizeableActivity="true"
            android:supportsPictureInPicture="true"
            android:theme="@style/Theme.AppCompat.FullScreen"
            tools:targetApi="n"/>

0 个答案:

没有答案