Open top menu




How to Integration YouTube channel in android app programmatic?


In this tutorial we will explain how to integrate youtube channel  in android and how to play YouTube videos in your app.

Step 1:-First you can enabled youtube API YouYube Android Player API  provide by google.

Please follow some Step and obtain google Developer API Key.

1. you can download API client library an JavaDocs.

2.Go to Google developer Console and create New Project and registering your application .

3.On the left-sidebar, select Dashboard and Enable API.

4.On the left-sidebar, select Library and select YouTube Data Api. then you check api enable or not.

5.On the left-sidebar, select Credential and Create new Key under public acess.







select Create Credential button then select RESTRIC KEY.

Open new settings..



select Android App Radio button and save. then generate API KEY and used this key in your android application..




6. Then you can add YouTubeAndroidApi.jar file in lib folder and compile in android studio and you can check in Gradle dependency add or not.


dependencies {
   .........
.......
......
    compile files('libs/YouTubeAndroidPlayerApi.jar')
}










7. Add Internet permission in manifest.xml file .


<uses-permission android:name="android.permission.INTERNET"/>





8. you can create Config  java class.
............................................................................................................
public class Config {
    public static final String _KEY= "AIzaSyDD5REJaO1Qu2y56v6QUW1FuGbnyariPI8";
    public static final String YOUTUBE_VIDEO_CODE = "CUciBrtqFGM";
}



............................................................................................................



9.you can create activity_main.xml
............................................................................................................
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
android:layout_height="match_parent" >


    
<LinearLayout        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"        
android:layout_centerInParent="true"        
android:layout_marginLeft="30dp"        
android:layout_marginRight="30dp"        
android:orientation="vertical" >

        
<LinearLayout            
android:layout_width="fill_parent"            
android:layout_height="wrap_content"            
android:gravity="center_horizontal"            
android:orientation="vertical" >

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

            
</com.google.android.youtube.player.YouTubePlayerView>

        
</LinearLayout>


    
</LinearLayout>

</RelativeLayout>
............................................................................................................

10. MainActivity.java
............................................................................................................

public class MainActivity extends YouTubeBaseActivity implements        YouTubePlayer.OnInitializedListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private YouTubePlayerView youTubeView;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize(Config._KEY, this);

    }

    @Override    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                        YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("some error...", errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer player, boolean wasRestored) {
        if (!wasRestored) {
            player.loadVideo(Config.YOUTUBE_VIDEO_CODE);
            player.setPlayerStyle(PlayerStyle.CHROMELESS);
        }
    }

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
        }
    }

    private YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }

}
............................................................................................................

0 comments