gpt4 book ai didi

android - 如何在Android的其他 fragment 之上播放youtube视频

转载 作者:行者123 更新时间:2023-12-03 06:18:36 25 4
gpt4 key购买 nike

我正在尝试使用其他片段在运行时在现有片段中播放youTube视频。我尝试跟随其他片段中的片段。但是我无法在以前的片段 View 中看到只有音频正在播放的视频。请帮帮我。我查询了约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();

最佳答案

当您使用嵌套片段时,请使用getChildFragmentManger()方法代替getSupportFragmentManager
Herehere还有更多信息

希望能帮助到你!

关于android - 如何在Android的其他 fragment 之上播放youtube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39998105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com