2

我有一个应用程序可以播放来自 youtube 的视频,并且我已经在 MainActivity 类中编写了链接。

我已经按预期在我的应用程序中播放了视频..

我试过PictureInPictureParams.Builder但遇到了多个错误..

public class MainActivity extends YouTubeBaseActivity implements 
YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
private MyPlayerStateChangeListener playerStateChangeListener;
private MyPlaybackEventListener playbackEventListener;
private YouTubePlayer player;

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

    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(Config.YOUTUBE_API_KEY, this);

    playerStateChangeListener = new MyPlayerStateChangeListener();
    playbackEventListener = new MyPlaybackEventListener();

    final EditText seekToText = (EditText) findViewById(R.id.seek_to_text);
    Button seekToButton = (Button) findViewById(R.id.seek_to_button);
    seekToButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int skipToSecs = Integer.valueOf(seekToText.getText().toString());
            player.seekToMillis(skipToSecs * 1000);
        }
    });
}

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    this.player = player;
    player.setPlayerStateChangeListener(playerStateChangeListener);
    player.setPlaybackEventListener(playbackEventListener);

    if (!wasRestored) {
        player.cueVideo("I0D-fkypQQw"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
    }
}

@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
    } else {
        String error = String.format(getString(R.string.player_error), errorReason.toString());
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RECOVERY_REQUEST) {
        // Retry initialization if user performed a recovery action
        getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);
    }
}

protected Provider getYouTubePlayerProvider() {
    return youTubeView;
}

private void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {

    @Override
    public void onPlaying() {
        // Called when playback starts, either due to user action or call to play().
        showMessage("Playing");
    }

    @Override
    public void onPaused() {
        // Called when playback is paused, either due to user action or call to pause().
        showMessage("Paused");
    }

    @Override
    public void onStopped() {
        // Called when playback stops for a reason other than being paused.
        showMessage("Stopped");
    }

    @Override
    public void onBuffering(boolean b) {
        // Called when buffering starts or ends.
    }

    @Override
    public void onSeekTo(int i) {
        // Called when a jump in playback position occurs, either
        // due to user scrubbing or call to seekRelativeMillis() or seekToMillis()
    }
}

private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {

    @Override
    public void onLoading() {
        // Called when the player is loading a video
        // At this point, it's not ready to accept commands affecting playback such as play() or pause()
    }

    @Override
    public void onLoaded(String s) {
        // Called when a video is done loading.
        // Playback methods such as play(), pause() or seekToMillis(int) may be called after this callback.
    }

    @Override
    public void onAdStarted() {
        // Called when playback of an advertisement starts.
    }

    @Override
    public void onVideoStarted() {
        // Called when playback of the video starts.
    }

    @Override
    public void onVideoEnded() {
        // Called when the video reaches its end.
    }

    @Override
    public void onError(YouTubePlayer.ErrorReason errorReason) {
        // Called when an error occurs.
    }
}
}

如何为视频缩小到应用程序右下角的视频实现画中画模式..

现在我得到的是在活动中播放的 youtube 视频。我期望的是让视频以画中画模式播放

4

1 回答 1

2

PictureInPictureParams我使用以下方法解决了我的问题

首先,我Button在我的 xml 中创建了一个(enter_pip),并在其中OnClick编写了以下代码:

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();
            }
        }

    });

我还在我的 AndroidManifest 中将以下属性添加到我的 PipActivity 以支持我的 Activity 中的画中画

<activity
        android:name=".PipActivity"
        android:launchMode="singleTask"
        android:supportsPictureInPicture="true"
        android:theme="@style/AppTheme.NoActionBar" />
于 2019-01-06T12:23:03.577 回答