0

我正在尝试以画中画模式播放 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"/>
4

0 回答 0