YouTube Android API:YouTubePlayerFragment加载微调器

前端之家收集整理的这篇文章主要介绍了YouTube Android API:YouTubePlayerFragment加载微调器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Android YouTube API示例在我的应用中创建无格式的YouTube播放器.我有一个问题,即缓冲/加载进度条即使在加载并开始播放后也会继续显示在我的视频上.我可以在FragmentDemoActivity示例中重现这一点,并进行一些小的修改
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,只是为了触发自动播放.

这种情况发生在包括Nexus 5X在内的多种设备上.我正在使用库版本1.2.2. onInitializationFailure中未触发任何错误.

视频在缓冲后开始播放.该播放器是无铬的.然而,缓冲旋转器永远不会消失.这是一个错误,还是我在做一些我不允许做的事情?

解决方法

我也遇到了这个,它看起来真的像个bug.以下是我设法解决它的方法.

在onInitializationSuccess中,在播放器上设置PlaybackEventListener.覆盖onBuffering并执行以下操作:

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代码更改时用作后备:

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,而在播放器缓冲时启用它.

编辑:如果使用此解决方案的任何人发现此错误已修复或ProgressBar的位置已更改,请分享,以便我可以编辑我的答案,谢谢!

猜你在找的Android相关文章