问题
I'm building a app that contains a YouTubePlayerView and YouTubePlayer, it works well in Portrait, Fullscreen-landscape but not landscape. It keeps pausing every second after I press play. The buffer shows it is fully loaded on some videos, yet it keeps pausing. I tested it on Android 2.2.2 and Android 4.1.2 with the latest version of the YouTube app. This happens in all player styles.
Thanks in advance.
回答1:
Make sure the size of the YoutubePlayer view is atleast 200x110 dp like descriped in the Youtube API documentation. Sounds like it doesn't meet those requirements when in landscape mode.
回答2:
I were facing same issues. but i am just using below concept it will work for me.
- take a linear layout in your activity layout xml.
<LinearLayout android:id="@+id/ll_youtube_player" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="@dimen/_55sdp" android:layout_marginStart="@dimen/_55sdp" android:layout_marginRight="@dimen/_55sdp" android:layout_marginEnd="@dimen/_55sdp" android:orientation="vertical" />
- Take a layout in which youtube player will be define layout_youtube_video_view_detail.xml.
<com.google.android.youtube.player.YouTubePlayerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/you_tube_player_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"/>
- In your activity inside onCreate() inflate youtube xml and add in your layout by code:
LinearLayout llYoutubePlayer = (LinearLayout)findViewById(R.id.ll_youtube_player);
youTubePlayerView = (YouTubePlayerView) getLayoutInflater().inflate(R.layout.layout_youtube_video_view_detail, null);
llYoutubePlayer.addView(youTubePlayerView);
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.you_tube_player_view);
youTubePlayerView.initialize(AppConstants.YOU_TUBE_API_KEY, this);
- in your onInitializationSuccess please pass video id.
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setPlayerStateChangeListener(playerStateChangeListener);
/** Start buffering **/
if (!wasRestored) {
player.loadVideo(videoId);
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
youTubePlayerView.refreshDrawableState();
}
}, AppConstants.DELAY_500);
}
I hope it will work.
来源:https://stackoverflow.com/questions/19981645/youtubeplayerview-keeps-pausing-in-landscape