3

I have a MenuActivity, in the MenuActivity i have a SeguridadMenuFrament and in the SeguridadMenuFrament i have SeguridadFragment. In the SeguridadFrament i need implement YoutubePlayer.

In my fragment_seguridad.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
    android:id="@+id/tvTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/TR_CARACTERISTICAS"
    android:textSize="@dimen/text_14"
    android:layout_marginTop="20dp"/>

    <TextView
    android:id="@+id/tvCaracteristicas"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/text_12"
        app:layout_constraintTop_toBottomOf="@id/tvTitle"
    android:layout_marginTop="20dp"/>

    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:id="@+id/youtubePF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@id/tvCaracteristicas"/>


</android.support.constraint.ConstraintLayout>

In the SeguridadFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_seguridad, container, false);

    mYoutubePlayerFragment = new YouTubePlayerSupportFragment();
    mYoutubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            if (!b) {
                youTubePlayer.cueVideo("NMwE93GQcKs");
            }
        }

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

        }
    });

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.youtubePF, mYoutubePlayerFragment);
    fragmentTransaction.commit();

    return rootView;
}

But when run the app and show the fragment, the app crash and show :

12-04 10:12:56.878 4342-4342/com.pixelcom.beta.pixelkartingsocios E/AndroidRuntime: FATAL EXCEPTION: main Process: com.pixelcom.beta.pixelkartingsocios, PID: 4342 java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.youtube.api.service.START } at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1373) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1482) at android.app.ContextImpl.bindService(ContextImpl.java:1450) at android.content.ContextWrapper.bindService(ContextWrapper.java:636) at com.google.android.youtube.player.internal.r.e(Unknown Source) at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source) at com.google.android.youtube.player.YouTubePlayerSupportFragment.a(Unknown Source) at com.google.android.youtube.player.YouTubePlayerSupportFragment.onCreateView(Unknown Source) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)*

Vivek Barai
  • 1,338
  • 13
  • 26
ignasivs
  • 51
  • 1
  • 8
  • have a look at my answer https://stackoverflow.com/a/26459181/909317 – Sunny Dec 04 '17 at 09:39
  • I need implement a video in a Fragment, not in Activity. In your answer, you implement a fragment in activity but i have a fragment. @Sunny – ignasivs Dec 04 '17 at 10:09
  • It's in fragment not in activity. – Sunny Dec 04 '17 at 10:52
  • You declare fragment in activity, but I declared a fragment in my SeguridadFragment (fragment). @Sunny – ignasivs Dec 04 '17 at 11:05
  • just declare a frame layout inside your fragment(SeguridadFragment) layout and use the code given in the link. – Sunny Dec 04 '17 at 11:17
  • @Sunny I tried this but i get this error: `Process: com.pixelcom.beta.pixelkartingsocios, PID: 15267 java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.youtube.api.service.START } at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1209)` What i can do? – ignasivs Dec 12 '17 at 16:35

1 Answers1

8

You need to use in your layout a FrameLayout instead a Fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/TR_CARACTERISTICAS"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"/>

    <TextView
    android:id="@+id/tvCaracteristicas"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/text_12"
        app:layout_constraintTop_toBottomOf="@id/tvTitle"
    android:layout_marginTop="20dp"/>

    <FrameLayout
        android:id="@+id/frame_fragment"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

</LinearLayout>

And then in your Fragment class use this code:

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_fragment, youTubePlayerFragment);
        transaction.commit();

        youTubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
                if (!b) {
                    YPlayer = youTubePlayer;
                    YPlayer.setFullscreen(false);
                    YPlayer.loadVideo("NMwE93GQcKs");
                    YPlayer.play();
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
                // TODO Auto-generated method stub

            }
        });

Hope it helps!

Marta Tenés
  • 2,102
  • 1
  • 13
  • 22
  • 1
    its working but video is getting stuck while playing.I couldn't sort out the problem :( – anju jo Sep 17 '18 at 05:33
  • @anjujo sorry for the late response. Have you tried with different videos and check your connection? Do you still have the problem? – Marta Tenés Sep 25 '18 at 06:53
  • yes..I have opened another question in SO.Could you please check it?"https://stackoverflow.com/questions/52361191/youtube-video-get-stuck-while-playing-in-fragment" – anju jo Sep 25 '18 at 06:57