YouTube Android API:YouTubePlayerFragment 加载微调器 [英] YouTube Android API: YouTubePlayerFragment loading spinner
问题描述
我正在使用 Android YouTube API 示例在我的应用中创建无 Chrome 的 YouTube 播放器.我有一个问题,即使在我的视频已加载并开始播放后,缓冲/加载进度条也会继续显示在我的视频上.我可以通过一些小的修改在 FragmentDemoActivity
示例中重现这一点:
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
.
视频在缓冲后开始播放.播放器是无铬的.然而,缓冲微调器永远不会消失.这是一个错误,还是我做了一些我不允许做的事情?
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?
推荐答案
我也遇到过这个问题,看起来确实是个bug.这是我设法解决它的方法.
I encountered this too, it really looks like a bug. Here is how I managed to work around it.
在您的 onInitializationSuccess
中,在 player
上设置一个 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);
// TODO 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 代码发生变化:
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;
}
这个解决方案对我来说非常好,当播放器正在缓冲时启用 ProgressBar
并在它没有时禁用它.
This solution works perfectly fine for me, enabling the ProgressBar
when the player is buffering and disabling it when it's not.
如果使用此解决方案的任何人发现此错误已修复或 ProgressBar
的位置已更改,请分享以便我可以编辑我的答案,谢谢!
If anyone using this solution discovers that this bug has been fixed or that the position of the ProgressBar
has changed, please share so that I can edit my answer, thanks!
这篇关于YouTube Android API:YouTubePlayerFragment 加载微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!