【问题标题】:How to play youtube video on top of other fragment in android如何在 android 中的其他片段之上播放 youtube 视频
【发布时间】:2017-02-21 05:25:43
【问题描述】:

我正在尝试使用其他片段在运行时在现有片段中播放 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();

【问题讨论】:

  • 嗨,你在哪里测试?在模拟器或真实设备中?您正在测试哪些版本的 Android?
  • 在真实设备上。安卓版本 6.0.1(棉花糖)

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


【解决方案1】:

当您使用嵌套片段时,请使用getChildFragmentManger() 方法而不是getSupportFragmentManager

Herehere 还有更多信息

希望对你有帮助!

【讨论】:

  • 你执行的是哪个类?在你的片段中?
  • 在一个活动中我称之为 youtube 片段。
  • 抱歉,我以为您使用的是嵌套片段。如果将 child_fragment 布局放在片段中并在该片段中调用 getchildfragmentmanager ,它可以工作吗?
  • :( ,我现在没有时间,但有时间我会尝试自己复制。对不起
  • 没关系。慢慢来,完成后告诉我:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 2012-11-28
  • 2017-12-23
  • 1970-01-01
相关资源
最近更新 更多