Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openin video inside app #15

Open
YourNeighbour opened this issue Dec 5, 2016 · 3 comments
Open

Openin video inside app #15

YourNeighbour opened this issue Dec 5, 2016 · 3 comments

Comments

@YourNeighbour
Copy link

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
Copy link

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
Copy link
Author

YourNeighbour commented Dec 8, 2016 via email

@muhakmal
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants