akoscz / YouTubePlaylist

A sample Android application which demonstrates the use of the YouTube Data v3 API.
Apache License 2.0
153 stars 84 forks source link

Openin video inside app #15

Open YourNeighbour opened 7 years ago

YourNeighbour commented 7 years ago

Hi. Im novice in Java. And I want to ask you know how can I view videos inside Android app and make it fullscreen after play-click.

vigneshwarn commented 7 years ago

Follow the below steps.

Step 1 - Create a Layout (Name it - activity_youtube_player_fragment.xml) and copy the below code

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true" />

Step 2 - Create a java file (Name it -YouTubePlayerFragmentActivity.java) and copy the below code

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class YouTubePlayerFragmentActivity extends YouTubeBaseActivity {
    public static final String API_KEY = "YOUR_APIKEY";

    String VIDEO_ID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        VIDEO_ID = extras.getString("id");
        setContentView(R.layout.activity_youtube_player_fragment);

        FragmentManager fm = getFragmentManager();
        String tag = YouTubePlayerFragment.class.getSimpleName();
        YouTubePlayerFragment playerFragment = (YouTubePlayerFragment) fm.findFragmentByTag(tag);
        if (playerFragment == null) {
            FragmentTransaction ft = fm.beginTransaction();
            playerFragment = YouTubePlayerFragment.newInstance();
            ft.add(android.R.id.content, playerFragment, tag);
            ft.commit();
        }

        playerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.setFullscreen(true);
                youTubePlayer.loadVideo(VIDEO_ID);
                youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                Toast.makeText(YouTubePlayerFragmentActivity.this, "Error while initializing YouTubePlayer.", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Step 3 - Add the below line on manifest before </application>

   <activity
            android:name=".YouTubePlayerFragmentActivity"
            android:hardwareAccelerated="true"
            android:screenOrientation="landscape" />

Step 4 - on PlaylistCardAdapter.java (Which already exist in this project ). On any .setOnClickListener add the below line.

Intent sendIntent = new Intent(holder.mContext, YouTubePlayerFragmentActivity.class);
                sendIntent.putExtra("id", video.getId());
                holder.mContext.startActivity(sendIntent);

This works for me. I hope it will work for you as well

YourNeighbour commented 7 years ago

Oh. Thanks. I'll try to do it, but only when I buy android phone. Yesterday, my phone was broken(

muhakmal commented 6 years ago

thanks @vigneshwarn it's clearly working for me. but how do i give the choice to non-full-screen/fullscreen option? thankyou.