如何在android中的其他片段上播放youtube视频

时间:2016-10-12 12:02:03

标签: android android-fragments youtube android-youtube-api

我正在尝试使用其他片段在运行时在现有片段中播放youTube视频。我尝试在其他片段中跟踪片段。但我无法看到视频,只有音频播放前一个片段视图。请帮帮我。我查询了大约200页,但没有任何对我有用。任何帮助将不胜感激。

这是我的 my_fragment.xml 代码:

<?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>

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);
    }
}

我正在使用此代码在运行时调用片段,

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();

1 个答案:

答案 0 :(得分:0)

在使用嵌套片段时,请使用getChildFragmentManger()方法代替getSupportFragmentManager

Herehere还有更多信息

希望它有所帮助!