使用YouTubePlayerFragment时出现空白黑匣子

时间:2014-03-11 19:33:56

标签: java android android-fragments

编辑 cjohn抓住了失踪的一块。我忘了在我的YTPlayerFragment上调用init()

Android dev新手。我试图将YouTube视频放在一个嵌套在另一个片段内的YouTubePlayerFragment中。 Logcat没有显示任何错误,但我的播放器片段只显示一个黑盒子。将日志语句放在我的OnInitializedListener中,它似乎甚至没有被调用。

包含YouTubePlayerFragment的ContentFragment是ViewPager中的一个标签。我的代码如下。 MainActivity ContentFragment,ContentFragment包含YouTubePlayerFragment。

MainActivity.java:

public class MainActivity extends FragmentActivity{

    TabsPagerAdapter mAdapter;
    ViewPager mViewPager;
    Boolean mLoggedIn;

    String[] mTabs = {"Library", "Content", "Quotes"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final ActionBar actionBar = getActionBar();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setAdapter(mAdapter);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }
        };

        for (String tab : mTabs) {
            actionBar.addTab(actionBar.newTab().setText(tab).setTabListener(tabListener));
        }


    }

}

TabsPagerAdapter.java:

public class TabsPagerAdapter extends FragmentPagerAdapter{
    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                return new LibraryFragment();
            case 1:
                return new ContentFragment();
            case 2:
                return new QuotesFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

ContentFragment.java:

public class ContentFragment extends Fragment {

    public ContentFragment() {
        // Required empty public constructor
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setRetainInstance(true);
        View v = inflater.inflate(R.layout.fragment_content, container, false);
        YTPlayerFragment video = YTPlayerFragment.newInstance("nCgQDjiotG0");
        getFragmentManager().beginTransaction().replace(R.id.youtubeFragment, video).commit();
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

YTPlayerFragment.java:

public class YTPlayerFragment extends YouTubePlayerSupportFragment {

    private YouTubePlayer activePlayer;
    private final static String DEV_KEY = "KEY";

    public static YTPlayerFragment newInstance(String url) {

        YTPlayerFragment player = new YTPlayerFragment();

        Bundle bundle = new Bundle();
        bundle.putString("url", url);

        player.setArguments(bundle);
        return player;
    }


    private void init() {
        initialize(DEV_KEY, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                activePlayer = youTubePlayer;
                Log.d("YOUTUBE", "Successfully initialized");
                activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                if (!b) {
                    activePlayer.cueVideo(getArguments().getString("url"));
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                Log.e("YOUTUBE", youTubeInitializationResult.toString());
            }
        });
    }


}

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="2dp"
              android:clipToPadding="false"
              android:scrollbarStyle="outsideOverlay"
              android:orientation="vertical" >
    <FrameLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/youtubeFragment" />

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

ContentFragment内,你正试图在孩提时添加另一个片段。要执行此操作,您需要通过调用 getChildFragmentManager() 获取的片段管理器:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setRetainInstance(true);
    View v = inflater.inflate(R.layout.fragment_content, container, false);
    YTPlayerFragment video = YTPlayerFragment.newInstance("nCgQDjiotG0");
    getChildFragmentManager().beginTransaction().replace(R.id.youtubeFragment, video).commit();
    return v;
}