问题
I am using Android YouTube API to show a video in portrait mode. Upon playing the video and turning the device into landscape mode, the video continues from the spot left off but re-buffers. I am looking for a solution as to how avoid the re-buffering.
I know for a fact its possible since Google provides an example here, and it works flawlessly except I can't seem to make it work with my application. I am unsure as to which functions to override and which ones actually remove the re-buffering.
Edit: here's the full xml for the cell in the ListView; the YouTubePlayerView is added programmatically after the last TextView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/youtube_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_shape"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/header_background"
android:weightSum="2" >
<TextView
android:id="@+id/youtube_username_tv"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top|left"
android:paddingLeft="15dp"
android:paddingTop="8dp"
android:paddingBottom="5dp"
android:textColor="@android:color/darker_gray"
android:textSize="13sp" />
<TextView
android:id="@+id/youtube_when_tv"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top|right"
android:paddingRight="15dp"
android:paddingTop="8dp"
android:paddingBottom="5dp"
android:textColor="@android:color/darker_gray"
android:textSize="13sp" />
</LinearLayout>
<TextView
android:id="@+id/youtube_content_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="17dp"
android:paddingTop="8dp"
android:paddingBottom="15dp"
android:textSize="15sp" />
</LinearLayout>
I know within java you'd need to override "onConfigurationChanged(Configuration newConfig)" and perhaps also "onFullscreen(boolean isFullscreen)", but just how exactly do you "grow" the youtube view without rebuffering? I tried using code from the full screen sample but it didn't seem to work out for me. Here is the player itself:
public class YouTubeParser extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener, YouTubePlayer.OnFullscreenListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
private String youtubeUrl;
private com.google.android.youtube.player.YouTubePlayerView youtube;
private YouTubePlayer player;
private Context mContext;
private boolean fullscreen;
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
public YouTubeParser(View v, Context context, Post post, String lastUrl) {
this.mContext = context;
youtubeUrl = post.getYoutubeUrl();
TextView username = (TextView) v.findViewById(R.id.youtube_username_tv);
TextView when = (TextView) v.findViewById(R.id.youtube_when_tv);
TextView content = (TextView) v.findViewById(R.id.youtube_content_tv);
if (username != null)
username.setText(post.getUsername());
if (when != null)
when.setText(post.getWhen());
if (content != null) {
content.setText(Html.fromHtml(post.getContent()));
content.setMovementMethod(LinkMovementMethod.getInstance());
}
if (lastUrl != youtubeUrl) {
youtube = new com.google.android.youtube.player.YouTubePlayerView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) LayoutParams.MATCH_PARENT, (int) LayoutParams.WRAP_CONTENT);
youtube.setLayoutParams(params);
LinearLayout ll = (LinearLayout) v.findViewById(R.id.youtube_ll);
if (ll.getChildCount() == 4)
ll.removeViewAt(2);
ll.addView(youtube, 2);
}
if (youtube != null) {
youtube.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(getString(0), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
this.player = player;
player.setPlayerStateChangeListener(this);
if (!wasRestored) {
player.cueVideo(youtubeUrl);
Log.i("Position", "video cued: " + youtubeUrl);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youtube;
}
public void setNoLandscape() {
if (player != null) {
int controlFlags = player.getFullscreenControlFlags();
controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
player.setFullscreenControlFlags(controlFlags);
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
player.pause();
}
}
public void setToLandscape() {
if (player != null) {
int controlFlags = player.getFullscreenControlFlags();
controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
player.setFullscreenControlFlags(controlFlags);
}
}
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
setNoLandscape();
}
@Override
public void onVideoStarted() {
int controlFlags = player.getFullscreenControlFlags();
controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
player.setFullscreenControlFlags(controlFlags);
}
@Override
public void onFullscreen(boolean isFullscreen) {
fullscreen = isFullscreen;
doLayout();
}
private void doLayout() {
LinearLayout.LayoutParams playerParams =
(LinearLayout.LayoutParams) youtube.getLayoutParams();
if (fullscreen) {
// When in fullscreen, the visibility of all other views than the player should be set to
// GONE and the player should be laid out across the whole screen.
playerParams.width = LayoutParams.MATCH_PARENT;
playerParams.height = LayoutParams.MATCH_PARENT;
} else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
playerParams.width = 0;
playerParams.height = WRAP_CONTENT;
playerParams.weight = 1;
} else {
playerParams.width = MATCH_PARENT;
playerParams.height = WRAP_CONTENT;
playerParams.weight = 0;
}
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
doLayout();
}
}
回答1:
by adding configChanges to you android manifest:
<activity android:name=".InfoActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name" >
</activity>
simply fix my problems:
- Starts buffering again
- Starts from the begining again
when screen was rotated.
Cheers.
来源:https://stackoverflow.com/questions/14691146/android-youtube-api-handling-orientation-change-with-no-buffering