Integrating youtube to fragment
First extend your Activity as normal
class YourActivity extends Activity...
in Layout file put the below lines
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then in your Activity you can create its instance using below line in your onCreareView method of your Fragment.
YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.youtube_fragment);
or you can declare a FrameLayout in your xml and then initiate the YouTubeSupportFragment
using below lines
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:visibility="visible" />
</RelativeLayout>
Code in your onCreateView
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
The key thing here is to use YouTubePlayerSupportFragment
instead of YouTubePlayerFragment
.
Hope this helps.
Here is your Fragment
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.ismart.omanapp.R;
public class YoutubeFragment extends Fragment {
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();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
}
}
To implement youtube player using fragment follow these 3 steps below:-
In the activity XML file add these:
<fragment android:name="com.google.android.youtube.player.YouTubePlayerFragment" android:id="@+id/youtube_fragment" android:layout_width="match_parent" android:layout_height="wrap_content"/>
In activity java file create a new OnInitialize listener
YouTubePlayer.OnInitializedListener onInitializedListener = new YouTubePlayer.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.loadVideo("Your_video_id"); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } };
After initializing the listener get the fragment reference and initialize the player.
YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment); youTubePlayerFragment.initialize("You_Developer_Api_Key", onInitializedListener);
To get the Developer API Key follow this link.