0

I am trying to play youTube video's within an existing fragment on runtime using other fragment. I try to follow the fragment within other fragment. But I am not able to see videos only audio is playing with the previous fragment view. Please help me out. I queried about 200 pages but nothing worked for me. Any help will be appreciated.

Here is my my_fragment.xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentOpponents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:keepScreenOn="true">

<android.support.v7.widget.RecyclerView
    android:id="@+id/opponents"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:numColumns="3" />

<FrameLayout
    android:id="@+id/child_fragment"
    android:layout_width="250dp"
    android:layout_height="250dp">
</FrameLayout>
</RelativeLayout>

Code for YoutubeFragment:

public class YoutubeFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_DIALOG_REQUEST = 1;

private static final String KEY_VIDEO_ID = "KEY_VIDEO_ID";

private String mVideoId;

//Empty constructor
public YoutubeFragment() {
}

/**
 * Returns a new instance of this Fragment
 *
 * @param videoId The ID of the video to play
 */
public static YoutubeFragment newInstance(final String videoId) {
    final YoutubeFragment youTubeFragment = new YoutubeFragment();
    final Bundle bundle = new Bundle();
    bundle.putString(KEY_VIDEO_ID, videoId);
    youTubeFragment.setArguments(bundle);
    return youTubeFragment;
}

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    final Bundle arguments = getArguments();

    if (bundle != null && bundle.containsKey(KEY_VIDEO_ID)) {
        mVideoId = bundle.getString(KEY_VIDEO_ID);
    } else if (arguments != null && arguments.containsKey(KEY_VIDEO_ID)) {
        mVideoId = arguments.getString(KEY_VIDEO_ID);
    }

    initialize(Consts.DEVELOPER_KEY, this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) {

    if (mVideoId != null) {
        if (restored) {
            youTubePlayer.play();
        } else {
            youTubePlayer.loadVideo(mVideoId);
        }
    }
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    if (youTubeInitializationResult.isUserRecoverableError()) {
        youTubeInitializationResult.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
    } else {
        //Handle the failure
        Toast.makeText(getActivity(), R.string.error_init_failure, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putString(KEY_VIDEO_ID, mVideoId);
    }
}

I am using this code to call a fragment on runtime,

FragmentManager fragmentManager = getSupportFragmentManager();
                   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                   Fragment fragment = YoutubeFragment.newInstance("q8_o4W9ZyZk");
                   fragmentTransaction.add(R.id.child_fragment, fragment);
                   fragmentTransaction.addToBackStack(null);
                   fragmentTransaction.commit();
Archana
  • 378
  • 3
  • 17

1 Answers1

0

As you are using nested fragments, use getChildFragmentManger() method instead getSupportFragmentManager

Here and here there is more info

Hope it helps!

Community
  • 1
  • 1
jos
  • 1,070
  • 12
  • 22