Youtube Player Integration with Thumbnail in Android App - android

Hi i follow tutorial on CodingwithMitch about using Youtube API in android. It is a good tutorial to start.
Everything is running perfectly however when the player is idle the screen is just blank instead of showing the video thumbnail/preview.
screenshot of my app
I would like to make the idle screen shows vid thumbnail so i know what video will be played.
The code in MyActivity.xml is below:
<view class="com.google.android.youtube.player.YouTubePlayerView"
android:id="#+id/youtubePlay"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
YouTube Config code:
public class YouTubeConfig {public YouTubeConfig() {} private static final String API_KEY = "AIzaSyXXXXXX";public static String getApiKey(){
return API_KEY;}}
And in my MainActivity.java
private static final String TAG = "MainActivity";
YouTubePlayerView mYoutubePlayerView;
Button btnPlay;
YouTubePlayer.OnInitializedListener mOnInitializedListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//PlayYouTube
Log.d(TAG, "onCreate: Starting.");
btnPlay = (Button) findViewById(R.id.btnPlay);
mYoutubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubePlay);
mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Log.d(TAG, "onInitializationSuccess: Done Initialising.");
youTubePlayer.loadPlaylist("PLZRTg9sNaMaFnh04-HODVcYRlFtKO8rsS");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG, "onInitializationSuccess: Fail to Initialize.");
}
};
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Initializing Youtube Player.");
mYoutubePlayerView.initialize(YouTubeConfig.getApiKey(), mOnInitializedListener);
}
});
Any help would be really appreciated

You need to supply the thumbnail for the youtube video that you want its preview to display.
Get a thumbnail from youtube video

Related

Minimizing/Maximizing youTube video in YouTubePlayerView

I have a container where I show a GIF video.On click of the GIF it opens the same video in YouTube. YouTubePlayerView has maximize and minimize icon, when I click on that video is just restarting instead of minimizing it and get back to the same container. I am not sure how to minimize the video which is playing in YouTubePlayerView.
I have created a separate Activity called YouTubeACtivity:
public class YoutubeActivity extends AppCompatActivity {
String shortCode = "";
public static void show(Activity activity, String shortCode)
{
Intent intent = new Intent(activity, YoutubeActivity.class);
intent.putExtra(Constants.YOUTUBE_VIDEO, shortCode);
activity.startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtubeplayer);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
shortCode = getIntent().getStringExtra(Constants.YOUTUBE_VIDEO);
YouTubePlayerFragment mYoutubePlayerFragment = new YouTubePlayerFragment().newInstance();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.youtubeContainer, mYoutubePlayerFragment);
ft.commit();
if (mYoutubePlayerFragment != null) {
mYoutubePlayerFragment.initialize(getResources().getString(R.string.google_api_key), new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) {
//youTubePlayer.setFullscreen(true);
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE| YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
// youTubePlayer. setShowFullscreenButton(false);
youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
#Override
public void onFullscreen(boolean fullscreen) {
// onBackPressed();
}
});
Log.e("shortcode ", "reached" + shortCode);
if (shortCode != null) {
if (restored) {
youTubePlayer.play();
} else {
youTubePlayer.loadVideo("" + shortCode + "");
}
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.e("youtube initialization ", "initialisation fails");
}
});
}
}
}
The GIF video is showing in a FrameLayout which implements SurfaceHolder callback.
I just wanted to know is there anyway we can make minimize the video and comeback to the container layout.
First I suggest to use the player object to go or exit fullscreen
youtubeplayer.setFullScreen(true/false);
then add config changes in manifest of the activity containing the container
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout"
as far as i can understand your problem your activity is getting restarted

GET YOUTUBE KEY_VIDEO_ID

How to get youtube key_vedio_id please help me to find-out this problem.
This is my code please and correct me if i wrong any where.
public class YouTubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
public static final String KEY_VIDEO_ID = "KEY_VIDEO_ID";
private String mVideoId;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_youtube);
final Bundle arguments = getIntent().getExtras();
if (arguments != null && arguments.containsKey(KEY_VIDEO_ID)) {
mVideoId = arguments.getString(KEY_VIDEO_ID);
}
final YouTubePlayerView playerView = (YouTubePlayerView) findViewById(R.id.youTubePlayerView);
playerView.initialize(getString(R.string.DEVELOPER_KEY_YOU_TUBE), this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) {
//Here we can set some flags on the player
//This flag tells the player to switch to landscape when in fullscreen, it will also return to portrait
//when leaving fullscreen
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
//This flag tells the player to automatically enter fullscreen when in landscape. Since we don't have
//landscape layout for this activity, this is a good way to allow the user rotate the video player.
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
//This flag controls the system UI such as the status and navigation bar, hiding and showing them
//alongside the player UI
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if (mVideoId != null) {
if (restored) {
youTubePlayer.play();
} else {
youTubePlayer.loadVideo(mVideoId);
}
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
//Handle the failure
Toast.makeText(this, R.string.error_init_failure, Toast.LENGTH_LONG).show();
}
}
}

list youtube playlist from my playlist with ID PLSt1J_r1AmrzOUCGZ6sWNxvOePK4nOwIk into my android application

List youtube playlist from my playlist with ID PLSt1J_r1AmrzOUCGZ6sWNxvOePK4nOwIk into my android application but it is showing one video at a time but on want it to show the whole list of videos before I play any video.
My main activity code
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String PlayList_ID = "PLP7qPet500dfglA7FFTxBmB_snxCaMHDJ";
private YouTubePlayer youTubePlayer;
private YouTubePlayerFragment youTubePlayerFragment;
private TextView textVideoLog;
private Button btnViewFullScreen;
private static final int RQS_ErrorDialog = 1;
String log = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager()
.findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerFragment.initialize(API_KEY, this);
textVideoLog = (TextView)findViewById(R.id.videolog);
btnViewFullScreen = (Button)findViewById(R.id.btnviewfullscreen);
btnViewFullScreen.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
youTubePlayer.setFullscreen(true);
}});
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
if (result.isUserRecoverableError()) {
result.getErrorDialog(this, RQS_ErrorDialog).show();
} else {
Toast.makeText(this,
"YouTubePlayer.onInitializationFailure(): " + result.toString(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()",
Toast.LENGTH_LONG).show();
if (!wasRestored) {
//player.cueVideo(VIDEO_ID);
player.cuePlaylist(PlayList_ID);
}
}
}
The playlist id, are the characters after "PL".
You can use recyclerView for the list. I have done this using Youtube api rest calls. You will need to get a browser key from the google developer console. I've explained this in my answer here:
https://stackoverflow.com/a/41201084/3689744

which file to render and play in android youtube player

I am successfully integrate YouTube Android Player API for my apps. I am curious to know about which file is rendered in the player. Every single video file may have several formats like 320dpi,720dpi & etc.
If my users, is in lower bandwidth; can i choose the file format or the API automatically detects which version would be played in that situation and vice-verse. My code:
public class YoutubeVideoActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, YouTubePlayer.OnFullscreenListener {
Activity activity = YoutubeVideoActivity.this;
public static final String API_KEY = "AIzaSyDN6Q9Pv4seQZqIcjB*********Po5k";
// public static final String VIDEO_ID = "psY0Botpi84";
public String new_id;
private boolean fullscreen;
private YouTubePlayerView playerView;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_youtube_video);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent intent = getIntent();
String video_link = intent.getExtras().getString("video_link");
try {
new_id = video_link.substring("http://www.youtube.com/watch?v="
.length());
if (new_id.equals("")) {
IndepententTVUtils.showCustomAlert(activity,
"Data is not availble. Press back key",
R.drawable.ic_new_launcher);
} else {
playerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
playerView.initialize(API_KEY, this);
}
} catch (Exception e) {
IndepententTVUtils.showCustomAlert(activity,
"Data is not availble", R.drawable.ic_new_launcher);
}
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
Toast.makeText(getApplicationContext(),
"To See this Video, Install Latest YouTube Application",
Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
boolean wasRestored) {
player.setOnFullscreenListener(this);
if (!wasRestored && new_id != null) {
player.cueVideo(new_id);
}
}
#Override
public void onFullscreen(boolean isFullscreen) {
fullscreen = isFullscreen;
}
}
The player sets video quality automatically according to the users bandwidth or internet speed, if it was not so there would be an option specifying the setPlaybackQuality in player.
There are only restricted options to change the player settings that are
here

Get youtube video time elapsed with new youtube API library

I want to play youtube videos using new Android Youtube API. I have download demo project from google (YouTubeAndroidAPIDemo) and this is the code I use to launch a video:
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, getString(R.string.ID_DEVELOPER_KEY), "cdgQpa1pUUE", 0, true, false);
startActivityForResult(intent, 1);
This running the youtube video ok, but, when the user press back key, I want to know time elapsed (for example, the youtube video size is 3:01, but the user only have seen 50 seconds)
How can I know time elapsed?
Thanks!
Finally I got my target creating another activity, put in a bundle the ID of the youtube video that I want to launch, and adding an atributte of the YouTubePlayer class, loading it in the onInitializationSuccess function. Here is my code:
public class Youtube_Activity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private String mVideo;
private int mInicio;
private YouTubePlayerView mYouTubeView;
private YouTubePlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube_activity);
Bundle extra = getIntent().getExtras();
mVideo = extra.getString("video");
mYouTubeView = (YouTubePlayerView)findViewById(R.id.youtube_view);
mYouTubeView.initialize(getString(R.string.ID_DEVELOPER_KEY), this);
}
#Override
public void onInitializationFailure(Provider provider,YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! "+error.toString(),Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,boolean wasRestored) {
player.loadVideo(mVideo);
mPlayer = player;
}
#Override
public void finish() {
Intent data = new Intent();
int t = (mPlayer.getCurrentTimeMillis()/1000);
data.putExtra("tiempo",t );
setResult(0, data);
super.finish();
}

Categories

Resources