java - YouTubePlayerView 画中画 android

标签 java android youtubeplayer

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.

if I activate the function without giving play to the video, the functionality is done correctly, but when activating the picture in picture functionality when the video is playing, when entering picture in picture, the video stops playing.

Image

自定义视频YoutubePlayer

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

}

Activity实现画中画功能:

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 库实现此功能吗?

最佳答案

我认为您还必须在 Activity 中重写 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.
     ...
  }
}

引用Android Documentation for Picture-in-picture

关于java - YouTubePlayerView 画中画 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51861035/

相关文章:

java - ...hbm.xml 文件在哪里?

java - HQL - 从带有点分隔符的选择中获取结果

java - 发现很长,需要 boolean 值(插入 SQLite [Android])

android - 如何获取前面TextView的Id?

flutter - 实现 youtube 播放器时 Flutter App 出错

ios - 不播放 youtube 视频 swift 5

java - 类似HPjmeter的图形工具查看-agentlib :hprof profiling output

java - 从字符串中获取多个int

Android - 创建 RESTful WebService - 在移动设备上托管