问题描述
我使用了Android的YouTube API样本来创建我的应用程序无边框YouTube播放器。我有一个问题,缓冲/加载进度条进行显示,即使在这之后已加载并开始播放我的视频。
I am using the Android YouTube API samples to create a chromeless YouTube player in my app. I am having an issue that the buffering / loading progress bar carries on displaying over my video even after it has loaded and started playing. I can reproduce this in the FragmentDemoActivity
sample with a couple of small modifications:
public class FragmentDemoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
player.loadVideo("nCgQDjiotG0", 10);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {}
}
我已经改变了 FragmentDemoActivity
从 AppCompatActivity
继承,而不是 YouTubeFailureRecoveryActivity
,如文档说的是细做。我也改变了玩家的风格是在 onInitializationSuccess
边框。最后,我已经改变了 cueVideo
到 loadVideo
,只是触发自动播放。
I have changed FragmentDemoActivity
to inherit from AppCompatActivity
instead of YouTubeFailureRecoveryActivity
, as the documentation says is fine to do. I have also changed the player style to be chromeless in onInitializationSuccess
. Finally, I have changed cueVideo
to loadVideo
, just to trigger auto play.
这发生在多台设备,包括Nexus 5X保护。我使用的库版本1.2.2。被触发 onInitializationFailure
没有错误。
This happens on multiple devices including Nexus 5X. I am using library version 1.2.2. No error is triggered in onInitializationFailure
.
视频开始缓冲后继续播放。该播放器是一个无镶边。然而,缓冲微调永不消失。这是一个bug,或我做的事情我不能做什么?
The video starts playing after buffering. The player is chromeless. However the buffering spinner never disappears. Is this a bug, or am I doing something I am not allowed to do?
推荐答案
我遇到过这个,它真的看起来像一个错误。这里是我设法解决它。
I encountered this too, it really looks like a bug. Here is how I managed to work around it.
在你的 onInitializationSuccess
,设置 PlaybackEventListener
在播放
。覆盖 onBuffering
键,做这样的事情:
In your onInitializationSuccess
, set a PlaybackEventListener
on the player
. Override the onBuffering
and do something like this:
ViewGroup ytView = (ViewGroup)ytPlayerFragment.getView();
ProgressBar progressBar;
try {
// As of 2016-02-16, the ProgressBar is at position 0 -> 3 -> 2 in the view tree of the Youtube Player Fragment
ViewGroup child1 = (ViewGroup)ytView.getChildAt(0);
ViewGroup child2 = (ViewGroup)child1.getChildAt(3);
progressBar = (ProgressBar)child2.getChildAt(2);
} catch (Throwable t) {
// As its position may change, we fallback to looking for it
progressBar = findProgressBar(ytView);
// I recommend reporting this problem so that you can update the code in the try branch: direct access is more efficient than searching for it
}
int visibility = isBuffering ? View.VISIBLE : View.INVISIBLE;
if (progressBar != null) {
progressBar.setVisibility(visibility);
// Note that you could store the ProgressBar instance somewhere from here, and use that later instead of accessing it again.
}
的 findProgressBar
方法,以防万一用作后备Youtube的code的变化:
The findProgressBar
method, used as a fallback just in case the Youtube code changes:
private ProgressBar findProgressBar(View view) {
if (view instanceof ProgressBar) {
return (ProgressBar)view;
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
ProgressBar res = findProgressBar(viewGroup.getChildAt(i));
if (res != null) return res;
}
}
return null
}
该解决方案对我的作品完美的罚款,使进度
当玩家缓冲和禁用它时,它不是。
This solution works perfectly fine for me, enabling the ProgressBar
when the player is buffering and disabling it when it's not.
这篇关于Android版YouTube API:YouTubePlayerFragment装载微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!