我正在使用youtube api播放器在我的应用程序中播放视频。
youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_view, youTubePlayerFragment).commit();
playerStateChangeListener = new MyPlayerStateChangeListener();
playbackEventListener = new MyPlaybackEventListener();
youTubePlayerFragment.initialize(ApplicationConstants.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
mplayer = player;
player.setFullscreen(false);
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
if (!wasRestored) {
player.cueVideo(url); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
}
我正在更改设备配置更改中的播放器角色,
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int config = newConfig.orientation;
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mYoutubeView.getLayoutParams();
switch (config){
case Configuration.ORIENTATION_LANDSCAPE :
mplayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
params.height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300,getResources().getDisplayMetrics());
params.width = LinearLayout.LayoutParams.WRAP_CONTENT ;//(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 375,getResources().getDisplayMetrics());
mYoutubeView.setLayoutParams(params);
break;
case Configuration.ORIENTATION_PORTRAIT :
params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
mYoutubeView.setLayoutParams(params);
break;
}
}
使用此标志我可以停止播放器api player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
但问题是,
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
标记,则可以避免此行为,但视频会不断停止。答案 0 :(得分:1)
尝试使用loadVideo()而不是cueVideo()