Play video from youtube in picture-in-picture mode

后端 未结 1 1681
天涯浪人
天涯浪人 2021-01-21 13:17

I have an app which plays video from youtube and i have coded the link in my MainActivity class..

I have made the video played in my app as expected..

I have t

相关标签:
1条回答
  • 2021-01-21 14:09

    I solved my problem using PictureInPictureParams as follows

    First i created a Button (enter_pip) in my xml and in the OnClick i coded the following :

    enter_pip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if (android.os.Build.VERSION.SDK_INT >= 26) {
                    //Trigger PiP mode
                    try {
                        Rational rational = new Rational(youTubeView.getWidth(), youTubeView.getHeight());
    
                        PictureInPictureParams mParams =
                                new PictureInPictureParams.Builder()
                                        .setAspectRatio(rational)
                                        .build();
    
                        enterPictureInPictureMode(mParams);
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "API 26 needed to perform PiP", Toast.LENGTH_SHORT).show();
                }
            }
    
        });
    

    I also added the following attributes in my AndroidManifest to my PipActivity to support Picture in Picture in my Activity

    <activity
            android:name=".PipActivity"
            android:launchMode="singleTask"
            android:supportsPictureInPicture="true"
            android:theme="@style/AppTheme.NoActionBar" />
    
    0 讨论(0)
提交回复
热议问题