我正在尝试在我的片段中播放视频。但是,我无法让它工作。如果我要扩展“YoutubeFailureRecovery”,我会得到:
09-06 21:56:40.472: E/AndroidRuntime(4946): Caused by: java.lang.IllegalStateException: YouTubePlayerView 只能使用扩展 YouTubeBaseActivity 作为其上下文的 Activity 创建。
这是我的 .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 中使用 YouTube API 有一些经验?