S
S
Sebastian Pereira2016-06-30 15:10:09
Android
Sebastian Pereira, 2016-06-30 15:10:09

How to implement Youtube player in fragment?

I need, depending on the checkbox, to display youtube or mediaplayer.
From FrameLayout I load fragment1.xml or fragment2.xml
main xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/chbStack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/stack">
        </CheckBox>
    </LinearLayout>
    <FrameLayout
        android:id="@+id/frgmCont"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

MainActivity
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frag1 = new Fragment1();
        frag2 = new Fragment2();

        chbStack = (CheckBox)findViewById(R.id.chbStack);
    }

    public void onClick(View v) {
        fTrans = getFragmentManager().beginTransaction();
        switch (v.getId()) {
            case R.id.btnAdd:
               fTrans.replace(R.id.frgmCont, frag1);
                break;
            case R.id.btnReplace:
                fTrans.replace(R.id.frgmCont, frag2);
            default:
                break;
        }
        if (chbStack.isChecked()) fTrans.addToBackStack(null);
        fTrans.commit();
    }

Fragment1 - there is a youtube player
public class Fragment1 extends Fragment implements YouTubePlayer.OnInitializedListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment1, container, false);
        YouTubePlayerFragment youtubePlayerFragment = new YouTubePlayerFragment();
        youtubePlayerFragment.initialize("api_key", this);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frgmCont, youtubePlayerFragment);
        fragmentTransaction.commit();
        return view;
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        youTubePlayer.cueVideo("EJylz_9KYf8");
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

    }
}

The problem is that youtube player is not initialized ;(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2016-06-30
@onepavel

getChildFragmentManager()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question