【问题标题】:YouTube Android fragment flickeringYouTube Android 片段闪烁
【发布时间】:2014-08-15 07:22:12
【问题描述】:

我正在使用官方 SDK 在我的 Android 应用中嵌入 Youtube 视频。问题是,每次初始化视频时,屏幕都会闪烁(黑色背景持续

XML:

        <LinearLayout
            android:id="@+id/youTube"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/cardui"
            android:orientation="vertical"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp"
            android:visibility="visible"
            tools:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_marginTop="5dp"
                android:fontFamily="sans-serif-light"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:text="Watch video"
                android:textAppearance="?android:attr/textAppearanceLarge" />

             <!-- This is where Youtube fragment will be added -->

          </LinearLayout>

Youtube 片段代码:

public static class PlayerYouTubeFrag extends YouTubePlayerSupportFragment {

    private String videoId;

    public static PlayerYouTubeFrag newInstance(String videoId) {

        PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();

        Bundle bundle = new Bundle();
        bundle.putString("videoId", videoId);
        playerYouTubeFrag.setArguments(bundle);

        return playerYouTubeFrag;
    }

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        init();

        return super.onCreateView(layoutInflater, viewGroup, bundle);
    }

    private void init() {
        videoId = getArguments().getString("videoId");
        initialize(youtubeKey, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youtubePlayer = youTubePlayer;
                youtubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                    @Override
                    public void onFullscreen(boolean b) {
                        isFullScreen = b;
                    }
                });

                if (!b) {
                    youtubePlayer.cueVideo(videoId);
                    youtubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                Log.d(TAG, "Player initialization failed");
            }
        });
    }
}

最后,将 Fragment 添加到布局中:

    mYoutubePlayerFragment = PlayerYouTubeFrag.newInstance(videoId);
    getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).add(R.id.youTube, mYoutubePlayerFragment).commit();
    (findViewById(R.id.youTube)).setVisibility(View.VISIBLE);

【问题讨论】:

    标签: android android-fragments youtube


    【解决方案1】:

    好的,这是因为 YouTubeSupportFragment 中有一个奇怪的错误,我不是第一个注意到它的人。你可以通过添加一个空的 SurfaceView 来解决这个问题。

    在这里阅读更多:

    http://code.google.com/p/gdata-issues/issues/detail?id=4722

    【讨论】:

      【解决方案2】:

      错误仍然存​​在,只是将SurfaceViewandroid:widthandroid:height 0dp 添加的解决方案并没有解决我的问题。相反,我必须在我的布局 xml 中添加 SurfaceView

      <SurfaceView
          android:id="@+id/surface"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:visibility="gone" />
      

      并且必须在添加 youtube 片段时将 SurfaceView 的可见性设置为在代码中可见。

      findViewById(R.id.surface).setVisibility(View.VISIBLE);
      getSupportFragmentManager().beginTransaction().replace(your_holder_id, youTubePlayerFragment).commit();
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 2011-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        相关资源
        最近更新 更多