I'm trying to play video in my Fragment. However, I cannot get it to work. If I'm extending 'YoutubeFailureRecovery' I get:
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.
This is my .xml:
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And this is the class:
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{
I just don't know what to do. I tried to extend my Fragment class with 'YoutubePlayerSupportFragment', tried to add a video-like fragment in XML, but nothing is working (error inflating). Maybe someone has some experience with using YouTube API in Fragment?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…