我正在使用Android YouTube API以纵向模式显示视频。播放视频并将设备切换到横向模式后,视频将从关闭但重新缓冲的位置继续播放。我正在寻找如何避免重新缓冲的解决方案。
我知道这是可能的,因为谷歌在这里提供了一个例子,它完美地工作,只是我似乎无法使它与我的应用程序一起工作。我不确定要覆盖哪些函数,以及哪些函数实际删除了重新缓冲。
编辑:这是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)",也许还需要覆盖"on Fullscreen(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();
}
}
通过添加configChanges到您的android清单:
<activity android:name=".InfoActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name" >
</activity>
简单地解决我的问题:
- 再次开始缓冲
- 从头再来
当屏幕旋转时。
干杯。