Open YourNeighbour opened 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
Oh. Thanks. I'll try to do it, but only when I buy android phone. Yesterday, my phone was broken(
thanks @vigneshwarn it's clearly working for me. but how do i give the choice to non-full-screen/fullscreen option? thankyou.
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.