Answer the question
In order to leave comments, you need to log in
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>
@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();
}
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) {
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question