YouTube Android API:YouTubePlayerFragment 加载微调器

2023-12-30

我正在使用 Android YouTube API 示例在我的应用程序中创建一个 chromeless 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 to loadVideo,只是为了触发自动播放。

这种情况发生在包括 Nexus 5X 在内的多种设备上。我正在使用库版本 1.2.2。没有错误被触发onInitializationFailure.

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


我也遇到了这个问题,确实是bug。这是我设法解决这个问题的方法。

In your onInitializationSuccess, set a PlaybackEventListener on the player。覆盖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.
}

The 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当播放器正在缓冲时,在不缓冲时禁用它。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

YouTube Android API:YouTubePlayerFragment 加载微调器 的相关文章

随机推荐