I'm making an application that implements the picture in picture functionality in android.
When you click on the image, open a new activity with the picture in picture functionality.
自定义videoYoutubePlayer
public class VideoYoutubeView extends RelativeLayout {
private final static String TAG = VideoYoutubeView.class.getName();
@BindView(R.id.youtube_player)
YouTubePlayerView youTubePlayerView;
@BindView(R.id.movie_play)
TextView btnPlay;
@BindView(R.id.movie_forward)
TextView btnForward;
@BindView(R.id.movie_back)
TextView btnBack;
@BindView(R.id.text_time_movie)
TextView textTimeVideo;
@BindView(R.id.seek_progress_movie)
SeekBar seekBarMovie;
@BindView(R.id.text_down)
TextView btnDown;
private YouTubePlayer mPlayer;
private Handler mHandler = null;
private OnPipListener onPipListener;
public VideoYoutubeView(Context context) {
super(context);
}
public VideoYoutubeView(Context context, AttributeSet attrs) {
super(context, attrs);
this.init(context);
}
public VideoYoutubeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.init(context);
}
public void setOnPipListener(@NonNull OnPipListener onPipListener) {
this.onPipListener = onPipListener;
}
public void init(Context context) {
View view = inflate(context, R.layout.youtube_view, this);
ButterKnife.bind(this, view);
this.mHandler = new Handler();
this.setTypeFace(context);
this.setUpVideoView();
}
private void setUpVideoView() {
final OnClickListener listener = view -> {
switch (view.getId()) {
case R.id.movie_play:
toggleMovie();
break;
case R.id.movie_back:
fastReview();
break;
case R.id.movie_forward:
fastForward();
break;
case R.id.text_down:
goToPip();
break;
}
};
this.btnPlay.setOnClickListener(listener);
this.btnForward.setOnClickListener(listener);
this.btnBack.setOnClickListener(listener);
this.btnDown.setOnClickListener(listener);
}
private void goToPip() {
if (this.onPipListener == null)
return;
this.onPipListener.goPiP();
}
public void hideControls() {
this.btnPlay.setVisibility(INVISIBLE);
this.btnDown.setVisibility(GONE);
this.textTimeVideo.setVisibility(GONE);
this.seekBarMovie.setVisibility(GONE);
}
public void showControls() {
this.btnPlay.setVisibility(VISIBLE);
this.btnDown.setVisibility(VISIBLE);
this.textTimeVideo.setVisibility(VISIBLE);
this.seekBarMovie.setVisibility(VISIBLE);
}
private void fastForward() {
}
private void fastReview() {
}
private void toggleMovie() {
if (this.mPlayer == null)
return;
if (this.mPlayer.isPlaying())
moviePause();
else
moviePlay();
}
private void moviePlay() {
if (this.mPlayer == null)
return;
this.mPlayer.play();
adjustStateMovie();
}
private void moviePause() {
if (this.mPlayer == null)
return;
this.mPlayer.pause();
adjustStateMovie();
}
private void adjustStateMovie() {
if (mPlayer == null)
return;
this.btnPlay.setText(mPlayer.isPlaying() ? R.string.iconPlay : R.string.iconPause);
}
private void setTypeFace(Context context) {
this.btnPlay.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
this.btnBack.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
this.btnForward.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
this.btnDown.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
}
public void initialize(String apiKey, String videoId) {
this.youTubePlayerView.initialize(apiKey, getOnInitializedListener(videoId));
}
private YouTubePlayer.OnInitializedListener getOnInitializedListener(String videoId) {
return new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean wasRestored) {
if (null == youTubePlayer) return;
mPlayer = youTubePlayer;
currentTimeVideo();
// Start buffering
if (!wasRestored) {
youTubePlayer.cueVideo(videoId);
}
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
// Add listeners to YouTubePlayer instance
youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
setSeekValue();
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
currentTimeVideo();
}
});
youTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
mHandler.removeCallbacks(runnable);
}
@Override
public void onPlaying() {
mHandler.postDelayed(runnable, 100);
currentTimeVideo();
}
@Override
public void onSeekTo(int arg0) {
mHandler.postDelayed(runnable, 100);
}
@Override
public void onStopped() {
mHandler.removeCallbacks(runnable);
}
});
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG, "onInitializationFailure: Error initialize video youtube.");
}
};
}
private void currentTimeVideo() {
if (null == mPlayer) return;
String formattedTime = Util.formatTimeVideo(mPlayer.getDurationMillis() - mPlayer.getCurrentTimeMillis());
textTimeVideo.setText(formattedTime);
}
private void setSeekValue() {
if (mPlayer == null)
return;
this.seekBarMovie.setMax(mPlayer.getDurationMillis());
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
currentTimeVideo();
mHandler.postDelayed(this, 100);
seekBarMovie.setProgress(mPlayer.getCurrentTimeMillis(), true);
}
};
public interface OnPipListener {
void goPiP();
}
}
活动实现画中画功能:
public class VideoActivity extends YouTubeBaseActivity {
@BindView(R.id.movie_chat)
VideoYoutubeView movieChat;
private final PictureInPictureParams.Builder mPictureInPictureParamsBuilder =
new PictureInPictureParams.Builder();
private VideoYoutubeView.OnPipListener onPipListener = (this::setUpPip);
public static Intent getCallIntent(Context context, String messageConversation) {
Intent intent = new Intent(context, VideoActivity.class);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
ButterKnife.bind(this);
this.setUpView();
}
private void setUpView() {
this.movieChat.initialize("", "JRfuAukYTKg");
this.movieChat.setOnPipListener(onPipListener);
}
public void setUpPip() {
if (movieChat == null)
return;
this.movieChat.hideControls();
//Rational aspectRatio = new Rational(movieChat.getWidth(), movieChat.getHeight());
Rational aspectRatio = new Rational(20, 10);
mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if (!isInPictureInPictureMode) {
// Show the video controls if the video is not playing
if (movieChat != null /*&& !movieChat.isPlaying()*/) {
movieChat.showControls();
}
}
}
}
有人可以通过YoutubeAndroidPlayerApi库帮助我实现此功能吗?
答案 0 :(得分:0)
我认为您还必须在活动中覆盖onPause()-
@Override
public void onPause() {
// If called while in PIP mode, do not pause playback
if (isInPictureInPictureMode()) {
// Continue playback
...
} else {
// Use existing playback logic for paused Activity behavior.
...
}
}