My Activity extends another activity and consists of navigation drawer where each item opens a new fragment.I want to integrate Youtube to one of the fragment..Previously i used YouTubePlayerView to integrate Youtube to activity but here it is required in fragment.I searched on net and found YouTubePlayerFragment to integrate youtube to fragment. But when i searched in tutorials I found that even by using YouTubePlayerFragment we are extending YouTubeBaseActivity .These are the examples..
http://android-coding.blogspot.in/2013/04/example-to-use-youtubeplayerfragment-of.html
http://android-er.blogspot.in/2013/06/example-to-use-youtubeplayerfragment-of.html
I failed to understand how to use YouTubePlayerFragment such that my class extends Fragment rather than YouTubeBaseActivity which is required in my project..As u can see below my class extends another activity and consists of navigation drawer in which fifth option opens YouTube Fragment.I want to play Youtube video inside this fragment..I am giving brief layout how my classes are-
public class LandingActivity extends BaseGActivity {
.
.
.
.
public void selectDrawerItem(int position) {
Bundle args = new Bundle();
switch (position) {
case 0:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_ALL);
GApplication.getInstance().stopGPlayer();
break;
case 1:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_PHOTO);
GApplication.getInstance().stopGPlayer();
break;
//
case 2:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_AUDIO);
GApplication.getInstance().stopGPlayer();
break;
case 3:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_VIDEO);
GApplication.getInstance().stopGPlayer();
break;
case 4:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_MEME);
GApplication.getInstance().stopGPlayer();
break;
case 5:
currentFragment = new YoutubeFragment();
default:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_ALL);
GApplication.getInstance().stopGPlayer();
break;
}
currentFragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(dataList.get(position).getItemName());
mDrawerLayout.closeDrawers();
}
public class YoutubeFragment extends Fragment implements YouTubePlayer.OnInitializedListener{
private FragmentActivity myContext;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);
YouTubePlayerView youTubeView = (YouTubePlayerView) rootView.findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, (YouTubePlayer.OnInitializedListener) myContext);
return rootView;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST)
{
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
}
YouTubeFragment.java
public class YoutubeFragment extends Fragment implements
YouTubePlayer.OnInitializedListener {
private FragmentActivity myContext;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize("DEVELOPER_KEY", new YouTubePlayer.OnInitializedListener() {
});
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
return rootView;
}
@Override
public void onInitializationSuccess (YouTubePlayer.Provider provider, YouTubePlayer
youTubePlayer,boolean b){
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure (YouTubePlayer.Provider
provider, YouTubeInitializationResult youTubeInitializationResult){
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Error-
Error:(64, 101) error: <anonymous com.pc.gi.ui.fragment.YoutubeFragment$1> is not abstract and does not override abstract method onInitializationFailure(Provider,YouTubeInitializationResult) in OnInitializedListener
See Question&Answers more detail:
os