Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to play Youtube video in your android app

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 476
    Comment on it

    In previous posts we discussed about how to generate  youtube video thumb and video_id. Now we are going to implement how to play videos in your android app by going through below easy steps.

    Step1: First adding YouTubeAndroidPlayerApi library in your android project.

        compile files('libs/YouTubeAndroidPlayerApi.jar')

    Step2: Adding FrameLayout to the xml file.

    <FrameLayout
            android:id="@+id/youtube_view"
            android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>

    Step3: Initializing YouTubePlayerSupportFragment and FrameLayout.

    private YouTubePlayerSupportFragment mYoutubePlayerFragment;
        private FrameLayout youtube_view;
    
        youtube_view = (FrameLayout)findViewById(R.id.youtube_view);
        mYoutubePlayerFragment = new YouTubePlayerSupportFragment();

    Step4: Now calling onInitializationSuccess and onInitializationFailure methods to play video.

    try{
                // Initializing video player with developer key
                mYoutubePlayerFragment.initialize(Config.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
    
                        if (!b) {
                            //showPlayer();
                            videoPlayer = youTubePlayer;
    
                            videoPlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
    
                                @Override
                                public void onFullscreen(boolean _isFullScreen) {
                                    fullScreen = _isFullScreen;
                                    VideoPlayerActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                                    if (!fullScreen) {
                                        videoPlayer.setFullscreen(false);
                                        VideoPlayerActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                                    }
                                }
                            });
                            videoPlayer.loadVideo(video_id);
                        } else {
                            VideoPlayerActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        }
                    }
    
                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                        if (youTubeInitializationResult.isUserRecoverableError()) {
                            youTubeInitializationResult.getErrorDialog(VideoPlayerActivity.this, RECOVERY_REQUEST).show();
                        } else {
                            String error = String.format(("Youtube player error"), youTubeInitializationResult.toString());
                            Toast.makeText(VideoPlayerActivity.this, error, Toast.LENGTH_LONG).show();
                        }
                    }
                });
    
                FragmentManager fragmentManager = VideoPlayerActivity.this.getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.youtube_view, mYoutubePlayerFragment);
                fragmentTransaction.commit();
    
            }catch (NullPointerException ex){
    		ex.printStackTrace();
            }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: