YouTube 播放器播放 2 秒后全屏停止
YouTube Player plays for 2 seconds and stop in full screen
我目前面临 2 个主要问题,
我正在使用 youtube 播放器,当它全屏播放时,播放 1-2 秒然后停止。
当我点击中间的"Play"按钮时,它又开始缓冲了。即使灰色条填充到它的中心。
纵向模式下不会出现这些问题。
这是我的 class,就像 youtube api 演示一样,有点不同
public class Video extends YouTubeFailureRecoveryActivity implements YouTubePlayer.OnFullscreenListener, Utils.OnGetUrlListener, View.OnClickListener {
static int AUTO_PLAY_DELAY = 1000;
static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
private LinearLayout mRootLayout;
/**
* * Youtube ***
*/
YouTubePlayerView mPlayerView;
YouTubePlayer mPlayer;
boolean mIsFullscreen;
String urlID;
/**
* * My ***
*/
RelativeLayout mContainer;
ImageView mBtPlay;
boolean mIsNeedSetFlags;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.getInstance().setActivity(this);
setContentView(R.layout.video_main);
mIsNeedSetFlags = true;
mRootLayout = (LinearLayout) findViewById(R.id.video_root_layout);
mContainer = (RelativeLayout) findViewById(R.id.container);
mBtPlay = (ImageView) findViewById(R.id.video_play);
mBtPlay.setVisibility(View.INVISIBLE);
mPlayerView = (YouTubePlayerView) findViewById(R.id.player);
Intent intent = getIntent();
doLayout();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
mPlayer = player;
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
player.setOnFullscreenListener(this);
if (mIsNeedSetFlags) {
mIsNeedSetFlags = false;
int controlFlags = player.getFullscreenControlFlags();
setRequestedOrientation(PORTRAIT_ORIENTATION);
controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
player.setFullscreenControlFlags(controlFlags);
}
if (!wasRestored) {
player.cueVideo(urlID);
}
}
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return mPlayerView;
}
private void doLayout() {
LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) mPlayerView.getLayoutParams();
if (mIsFullscreen) {
playerParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
playerParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
mParallaxScrollView.setVisibility(View.GONE);
} else {
mParallaxScrollView.setVisibility(View.VISIBLE);
if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
ViewGroup.LayoutParams otherViewsParams = mParallaxScrollView.getLayoutParams();
playerParams.width = otherViewsParams.width = MATCH_PARENT;
playerParams.height = WRAP_CONTENT;
mRootLayout.setOrientation(LinearLayout.VERTICAL);
}
}
mPlayerView.setLayoutParams(playerParams);
}
@Override
public void onFullscreen(boolean isFullscreen) {
mIsFullscreen = isFullscreen;
showPlayerAndPlay();
doLayout();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
doLayout();
}
@Override
public void onGetUrlFinished(String videoUrl) {
urlID = videoUrl;
mBtPlay.setVisibility(View.VISIBLE);
mBtPlay.setOnClickListener(this);
mPlayerView.initialize(Utils.YOU_TUBE_DEV_KEY, this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.video_play:
showPlayerAndPlay();
break;
}
}
private void showPlayerAndPlay() {
mPlayerView.setVisibility(View.VISIBLE);
mBtPlay.setVisibility(View.INVISIBLE);
if (!mPlayer.isPlaying())
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
mPlayer.play();
}
}, AUTO_PLAY_DELAY);
}
}
YouTube 不允许其他视图覆盖其播放器视图。
如果您查看日志,您还会看到一条警告消息,指出了这一限制,以及关于哪个视图(其 ID)和重叠区域的更多信息。
一个很好的替代方法是使用 Exoplayer,用视图覆盖您的视频。它不是 android sdk 的一部分,但 google 推荐并包含在 android 开发人员文档中:
http://google.github.io/ExoPlayer/ https://developer.android.com/guide/topics/media/exoplayer.html
Exoplayer 允许您流式传输任何类型的视频,而不仅仅是 Youtube 视频。
还值得一提的是,在 Youtube 应用程序中使用了 Exoplayer。
正如标记为正确的答案所解释的那样:问题在于将视图覆盖在 Youtube 播放器视图上。 如果 Youtube 正在初始化时您需要保留这些视图,那么这就可以了。
我正在为所涉及的视图制作一个带有交叉淡入淡出的加载动画。将 alpha 设置为 0 不会解决问题,因为视图仍然存在。但是将可见性设置为 GONE
或 INVISIBLE
确实有效。据我了解,如果不是 VISIBLE
,则不会计算 View
,至少在可见性更改后不会考虑它。最后,我做了这样的事情:
myView.animate().alpha(0).setDuration(800).setListener(
new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
myView.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
}).start();
我目前面临 2 个主要问题,
我正在使用 youtube 播放器,当它全屏播放时,播放 1-2 秒然后停止。
当我点击中间的"Play"按钮时,它又开始缓冲了。即使灰色条填充到它的中心。
纵向模式下不会出现这些问题。
这是我的 class,就像 youtube api 演示一样,有点不同
public class Video extends YouTubeFailureRecoveryActivity implements YouTubePlayer.OnFullscreenListener, Utils.OnGetUrlListener, View.OnClickListener {
static int AUTO_PLAY_DELAY = 1000;
static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
private LinearLayout mRootLayout;
/**
* * Youtube ***
*/
YouTubePlayerView mPlayerView;
YouTubePlayer mPlayer;
boolean mIsFullscreen;
String urlID;
/**
* * My ***
*/
RelativeLayout mContainer;
ImageView mBtPlay;
boolean mIsNeedSetFlags;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.getInstance().setActivity(this);
setContentView(R.layout.video_main);
mIsNeedSetFlags = true;
mRootLayout = (LinearLayout) findViewById(R.id.video_root_layout);
mContainer = (RelativeLayout) findViewById(R.id.container);
mBtPlay = (ImageView) findViewById(R.id.video_play);
mBtPlay.setVisibility(View.INVISIBLE);
mPlayerView = (YouTubePlayerView) findViewById(R.id.player);
Intent intent = getIntent();
doLayout();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
mPlayer = player;
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
player.setOnFullscreenListener(this);
if (mIsNeedSetFlags) {
mIsNeedSetFlags = false;
int controlFlags = player.getFullscreenControlFlags();
setRequestedOrientation(PORTRAIT_ORIENTATION);
controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
player.setFullscreenControlFlags(controlFlags);
}
if (!wasRestored) {
player.cueVideo(urlID);
}
}
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return mPlayerView;
}
private void doLayout() {
LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) mPlayerView.getLayoutParams();
if (mIsFullscreen) {
playerParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
playerParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
mParallaxScrollView.setVisibility(View.GONE);
} else {
mParallaxScrollView.setVisibility(View.VISIBLE);
if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
ViewGroup.LayoutParams otherViewsParams = mParallaxScrollView.getLayoutParams();
playerParams.width = otherViewsParams.width = MATCH_PARENT;
playerParams.height = WRAP_CONTENT;
mRootLayout.setOrientation(LinearLayout.VERTICAL);
}
}
mPlayerView.setLayoutParams(playerParams);
}
@Override
public void onFullscreen(boolean isFullscreen) {
mIsFullscreen = isFullscreen;
showPlayerAndPlay();
doLayout();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
doLayout();
}
@Override
public void onGetUrlFinished(String videoUrl) {
urlID = videoUrl;
mBtPlay.setVisibility(View.VISIBLE);
mBtPlay.setOnClickListener(this);
mPlayerView.initialize(Utils.YOU_TUBE_DEV_KEY, this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.video_play:
showPlayerAndPlay();
break;
}
}
private void showPlayerAndPlay() {
mPlayerView.setVisibility(View.VISIBLE);
mBtPlay.setVisibility(View.INVISIBLE);
if (!mPlayer.isPlaying())
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
mPlayer.play();
}
}, AUTO_PLAY_DELAY);
}
}
YouTube 不允许其他视图覆盖其播放器视图。
如果您查看日志,您还会看到一条警告消息,指出了这一限制,以及关于哪个视图(其 ID)和重叠区域的更多信息。
一个很好的替代方法是使用 Exoplayer,用视图覆盖您的视频。它不是 android sdk 的一部分,但 google 推荐并包含在 android 开发人员文档中:
http://google.github.io/ExoPlayer/ https://developer.android.com/guide/topics/media/exoplayer.html
Exoplayer 允许您流式传输任何类型的视频,而不仅仅是 Youtube 视频。
还值得一提的是,在 Youtube 应用程序中使用了 Exoplayer。
正如标记为正确的答案所解释的那样:问题在于将视图覆盖在 Youtube 播放器视图上。 如果 Youtube 正在初始化时您需要保留这些视图,那么这就可以了。
我正在为所涉及的视图制作一个带有交叉淡入淡出的加载动画。将 alpha 设置为 0 不会解决问题,因为视图仍然存在。但是将可见性设置为 GONE
或 INVISIBLE
确实有效。据我了解,如果不是 VISIBLE
,则不会计算 View
,至少在可见性更改后不会考虑它。最后,我做了这样的事情:
myView.animate().alpha(0).setDuration(800).setListener(
new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
myView.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
}).start();