Customizing youtube player view in android - android

i am using an youtube player api of google to play youtube videos, it specifies three different styles of player view, is this any way to customize the player view in android.
Thank you

I don't know what kind of style you want, but you can use YouTube Player as Activity or Fragment or a View.
So you can basically customise your player easily with new YouTubePlayer API
If you want some simple youtube player with fullscreen (no titlebar mode), You are welcome to use my code!!
This source handle "Orientation Problem", "Media Volume Problem", "Youtube Url Parsing Problem"
Here is open source library
https://github.com/TheFinestArtist/SimpleYouTubePlayer
This is sample codes
https://gist.github.com/TheFinestArtist/5545437
I also made sample app you can download
https://play.google.com/store/apps/details?id=com.thefinestartist.simpleyoutubeplayer

Try this link for youtube video play in my layout : http://www.techrepublic.com/blog/software-engineer/using-googles-youtube-api-in-your-android-apps/
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "add your own key here!";
static private final String VIDEO = "4SK0cUNMnMM";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);
youTubeView.initialize(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(VIDEO);
}
}

You can pick he chromeless style, it doesn't have any controls - it's only a rectangle that plays a video under programmatic control.
You're free to implement your own controls UI outside the player and wire them up through the YouTubePlayer that you get back after initializing a YouTubePlayerView or YouTubePlayerFragment. Keep in mind that, as mentioned in the YouTubePlayerView JavaDoc, "it is not permitted to overlay the [player] view with other views while a video is playing"

FYI: you can use PopupWindow to workaround restriction: "it is not permitted to overlay the [player] view with other views while a video is playing"

Related

YouTube Player View pausing video after each second

I understand that there are other similar questions asked. But they either have no solutions or the solutions provided are not working.
The player works fine in fullscreen mode but as a part of the layout contents, it keeps pausing and rebuffering.
Here is my code:
public class PlayerActivity extends YouTubeBaseActivity implements MediaPlayer.OnPreparedListener, YouTubePlayer.OnInitializedListener {
...
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.player_youtube);
Button yt_player_button = (Button) findViewById(R.id.player_yt_play);
yt_player_button.setText("Click to play a Youtube video");
yt_player_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
youTubePlayerView.initialize(getString(R.string.youtube_api_key), PlayerActivity.this);
}
});
}
...
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (youTubePlayer == null)
return;
if (!b)
youTubePlayer.cueVideo("63kmMcHBQlA");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
YoutubePlayerView cannot be nested inside a ScrollView for whatever the reason.
It just doesn't work. I suggest that you create a fragment which pops up during the request for the Youtube video or creating a new intent with just the YoutubePlayerView (maybe fullscreen).
I couldn't find a reason as to why this is the case.
(Please do add it as an answer if you know why)
Using exo-player is a very good alternative too (If you can get the DASH Url's in a dynamic app. Then best of luck)

How to next Youtube Video using Android Player API?

I developed an app using YouTube DATA API. I play single video but i want to use the next button to next the video. I use YouTube player to play video & i pass the video id to the video player.
Now How can i next the video?
here is my code what i am using to play single video
public class PlayerViewDemoActivity extends YouTubeFailureRecoveryActivity {
private String vedio_id;
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
requestWindowFeature(1);
setContentView(R.layout.playerview_demo);
this.vedio_id = getIntent().getStringExtra("videoId");
((YouTubePlayerView) findViewById(R.id.youtube_view)).initialize("AIzaSyCojCp66RLS9OY8hOwnW0UWLNdC56z24Os", this);
}
public void onInitializationSuccess(YouTubePlayer.Provider paramProvider, YouTubePlayer paramYouTubePlayer, boolean paramBoolean) {
if (!paramBoolean) {
paramYouTubePlayer.loadVideo(this.vedio_id);
paramYouTubePlayer.play();
}
}
}
if you only pass the video id to the video player you have to pass the new video id to play new.
paramYouTubePlayer.loadVideo(idVideo);
In your example you have to create a new attribute private YoutubePlayer paramYoutubePlayer;.
Next, you can create a new method like this:
public void nextVideo (String idVideo)
{
paramYouTubePlayer.loadVideo(idVideo);
paramYouTubePlayer.play();
}
But you can load an array of videos with:
loadVideos(List<String> videoIds)
and once you have loaded the List, you can call the next() and previous() methods on the youtubePlayer object.
Hope it helps!

YoutubeApi loader doesn't disppear [duplicate]

I am using the Android YouTube API samples to create a chromeless YouTube player in my app. I am having an issue that the buffering / loading progress bar carries on displaying over my video even after it has loaded and started playing. I can reproduce this in the FragmentDemoActivity sample with a couple of small modifications:
public class FragmentDemoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
player.loadVideo("nCgQDjiotG0", 10);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {}
}
I have changed FragmentDemoActivity to inherit from AppCompatActivity instead of YouTubeFailureRecoveryActivity, as the documentation says is fine to do. I have also changed the player style to be chromeless in onInitializationSuccess. Finally, I have changed cueVideo to loadVideo, just to trigger auto play.
This happens on multiple devices including Nexus 5X. I am using library version 1.2.2. No error is triggered in onInitializationFailure.
The video starts playing after buffering. The player is chromeless. However the buffering spinner never disappears. Is this a bug, or am I doing something I am not allowed to do?
I encountered this too, it really looks like a bug. Here is how I managed to work around it.
In your onInitializationSuccess, set a PlaybackEventListener on the player. Override the onBuffering and do something like this:
ViewGroup ytView = (ViewGroup)ytPlayerFragment.getView();
ProgressBar progressBar;
try {
// As of 2016-02-16, the ProgressBar is at position 0 -> 3 -> 2 in the view tree of the Youtube Player Fragment
ViewGroup child1 = (ViewGroup)ytView.getChildAt(0);
ViewGroup child2 = (ViewGroup)child1.getChildAt(3);
progressBar = (ProgressBar)child2.getChildAt(2);
} catch (Throwable t) {
// As its position may change, we fallback to looking for it
progressBar = findProgressBar(ytView);
// TODO I recommend reporting this problem so that you can update the code in the try branch: direct access is more efficient than searching for it
}
int visibility = isBuffering ? View.VISIBLE : View.INVISIBLE;
if (progressBar != null) {
progressBar.setVisibility(visibility);
// Note that you could store the ProgressBar instance somewhere from here, and use that later instead of accessing it again.
}
The findProgressBar method, used as a fallback just in case the YouTube code changes:
private ProgressBar findProgressBar(View view) {
if (view instanceof ProgressBar) {
return (ProgressBar)view;
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
ProgressBar res = findProgressBar(viewGroup.getChildAt(i));
if (res != null) return res;
}
}
return null;
}
This solution works perfectly fine for me, enabling the ProgressBar when the player is buffering and disabling it when it's not.
EDIT: If anyone using this solution discovers that this bug has been fixed or that the position of the ProgressBar has changed, please share so that I can edit my answer, thanks!

YouTube Android API: YouTubePlayerFragment loading spinner

I am using the Android YouTube API samples to create a chromeless YouTube player in my app. I am having an issue that the buffering / loading progress bar carries on displaying over my video even after it has loaded and started playing. I can reproduce this in the FragmentDemoActivity sample with a couple of small modifications:
public class FragmentDemoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
player.loadVideo("nCgQDjiotG0", 10);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {}
}
I have changed FragmentDemoActivity to inherit from AppCompatActivity instead of YouTubeFailureRecoveryActivity, as the documentation says is fine to do. I have also changed the player style to be chromeless in onInitializationSuccess. Finally, I have changed cueVideo to loadVideo, just to trigger auto play.
This happens on multiple devices including Nexus 5X. I am using library version 1.2.2. No error is triggered in onInitializationFailure.
The video starts playing after buffering. The player is chromeless. However the buffering spinner never disappears. Is this a bug, or am I doing something I am not allowed to do?
I encountered this too, it really looks like a bug. Here is how I managed to work around it.
In your onInitializationSuccess, set a PlaybackEventListener on the player. Override the onBuffering and do something like this:
ViewGroup ytView = (ViewGroup)ytPlayerFragment.getView();
ProgressBar progressBar;
try {
// As of 2016-02-16, the ProgressBar is at position 0 -> 3 -> 2 in the view tree of the Youtube Player Fragment
ViewGroup child1 = (ViewGroup)ytView.getChildAt(0);
ViewGroup child2 = (ViewGroup)child1.getChildAt(3);
progressBar = (ProgressBar)child2.getChildAt(2);
} catch (Throwable t) {
// As its position may change, we fallback to looking for it
progressBar = findProgressBar(ytView);
// TODO I recommend reporting this problem so that you can update the code in the try branch: direct access is more efficient than searching for it
}
int visibility = isBuffering ? View.VISIBLE : View.INVISIBLE;
if (progressBar != null) {
progressBar.setVisibility(visibility);
// Note that you could store the ProgressBar instance somewhere from here, and use that later instead of accessing it again.
}
The findProgressBar method, used as a fallback just in case the YouTube code changes:
private ProgressBar findProgressBar(View view) {
if (view instanceof ProgressBar) {
return (ProgressBar)view;
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
ProgressBar res = findProgressBar(viewGroup.getChildAt(i));
if (res != null) return res;
}
}
return null;
}
This solution works perfectly fine for me, enabling the ProgressBar when the player is buffering and disabling it when it's not.
EDIT: If anyone using this solution discovers that this bug has been fixed or that the position of the ProgressBar has changed, please share so that I can edit my answer, thanks!

why setOnInfoListener not supported for VideoView?

I am creating an application in which I am using VideoView to stream video from URL Source. The video is taking some time to buffer and play so I set onPrepareListener to VideoView and I display Progress Dialogue while it buffers and remove it when the playing starts. But again after sometime it starts buffering.I want to show Progress Dialogue again. I had earlier used OInfoListener with MediaPlayer to listen to buffering,but with VideoView I am not able to set OInfoListener. I want to know is it possible to setOnInfoListener with 'VideoView`. If not then how to achieve my requirement.
#Override
public void setOnCompletionListener(OnCompletionListener l) {
super.setOnCompletionListener(l);
}
#Override
public void setOnErrorListener(OnErrorListener l) {
super.setOnErrorListener(l);
}
#Override
public void setOnPreparedListener(OnPreparedListener l) {
super.setOnPreparedListener(l);
}
these are the listeners available for videoview

Categories

Resources