gpt4 book ai didi

android - 如何使用 YouTubePlayerFragment 通过 YouTube API 播放视频?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:44 25 4
gpt4 key购买 nike

我正在尝试在我的 Fragment 中播放视频。但是,我无法让它工作。如果我扩展“YoutubeFailureRecovery”,我会得到:

09-06 21:56:40.472: E/AndroidRuntime(4946): Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.

这是我的 .xml:

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

这是类:

public class YoutubeFragment extends YoutubeFailureRecovery {

YouTubePlayer player;
YouTubePlayerView playerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle arg2) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.detailview, container, false);
}

@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
playerView = (YouTubePlayerView)getActivity().findViewById(R.id.player);
playerView.initialize(DataHolder.DEVELOPER_KEY, this);

}

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
// TODO Auto-generated method stub
player.cueVideo("nCgQDjiotG0");
}

@Override
protected Provider getYouTubePlayerProvider() {
// TODO Auto-generated method stub
return playerView;
}
}


public abstract class YoutubeFailureRecovery extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener{

我只是不知道该怎么办。我尝试使用“YoutubePlayerSupportFragment”扩展我的 Fragment 类,尝试在 XML 中添加类似视频的 fragment ,但没有任何效果(错误膨胀)。也许有人有在 Fragment 中使用 YouTube API 的经验?

最佳答案

The YouTubePlayerView only works with an Activity that extends YouTubeBaseActivity.

如果您想使用 fragment 您必须使用 YoutubePlayerFragment/SupportFragment。

例如,您可以创建继承自 YoutubePlayerSupportFragment 的自定义 fragment :

public class VideoFragment extends YouTubePlayerSupportFragment {

public VideoFragment() { }

public static VideoFragment newInstance(String url) {

VideoFragment f = new VideoFragment();

Bundle b = new Bundle();
b.putString("url", url);

f.setArguments(b);
f.init();

return f;
}

private void init() {

initialize("yourapikey", new OnInitializedListener() {

@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(getArguments().getString("url"));
}
}
});
}
}

在代码中它可能是这样的:

VideoFragment f = VideoFragment.newInstance("your-video-url");   
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();

本例中的“fragment_container”应该是一个空的FrameLayout

关于android - 如何使用 YouTubePlayerFragment 通过 YouTube API 播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18664934/

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