我正在使用Android YouTube API以纵向模式显示视频。在播放视频并将设备转为横向模式时,视频会从离开的位置继续播放,但会重新缓冲。我正在寻找解决方案来避免重新缓冲。
我知道这是可能的,因为Google提供了一个示例here,它可以完美地工作,但我似乎无法使其与我的应用程序一起工作。我不确定要覆盖哪些函数以及哪些函数实际上会消除重新缓冲。
编辑:这是ListView中单元格的完整xml;YouTubePlayerView是在最后一个TextView之后以编程方式添加的。
我知道这是可能的,因为Google提供了一个示例here,它可以完美地工作,但我似乎无法使其与我的应用程序一起工作。我不确定要覆盖哪些函数以及哪些函数实际上会消除重新缓冲。
编辑:这是ListView中单元格的完整xml;YouTubePlayerView是在最后一个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>
我知道在Java中,您需要重写“onConfigurationChanged(Configuration newConfig)”,也许还需要“onFullscreen(boolean isFullscreen)”,但是如何在不重新缓冲的情况下“扩展”YouTube视图?我尝试使用全屏示例中的代码,但似乎对我没有用。这是播放器本身:
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();
}
}