Stopping the youtube player playback custom view - android

I am using a com.google.android.youtube.player.YouTubePlayerView to play my youtube videos, I extend YouTubeBaseActivity. I am having an issue where I call .pause() on the youtube player instance and and set the view VISIBILITY to View.GONE but it keeps buffering after the view has been set to View.GONE and throws and error because it cannot play in a hidden view. I called YouTubePlayer.pause() before hiding the view. Is there another way to stop playback? I want to stop the player and remove the com.google.android.youtube.player.YouTubePlayerView when a button is pressed. Thanks for taking a look.
if(youTubePlayer.isPlaying()){
youTubePlayer.pause();
youTubePlayerView.setVisibility(View.GONE); }

The pause function will will not stop it. You should try to release it for stopping it. Try the following code
youTubePlayer.release();

It's too late to answer - But still, someone might be benefited -
You can set player style in youtube player.
youtubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT) // To set player as youtube provided.
youtubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL) // To set minimal version of the youtube player. In this style you'll not get the player control options.
youtubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL) This line will do the work of Stopping the youtube player playback custom view

Related

How to check if webview YouTube video player was started

In my Android app, I have a webview where there is an embedded YouTube video inside the webview. My app has a native AdMob banner.
I'd like to hide the native admob banner from the app when user plays the video, so the banner does not show while the video is playing and then show the ads again when the video stops playing
The issue is that I do not know how to check if the video was started or stopped.
Any idea how this can be done? Thanks much.
One of the suggestions I think of is to set your AdView's visibility to GONE and also call mAdView.pause() the AdView while the video is playing. This should prevent any additional requests being made to AdMob. Once the video is done playing and you want to show your banner again - you should set the AdView's visibility to VISIBLE and call mAdView.resume()
The communication between webview and native are done through JavascriptInterface. YouTube has built it's API in a similar fashion. you can use the below code to achieve what you want.
To achieve the play/pause functionality via Youtube video you can use YouTube JavaScript Player API.
Example:
<div id="video-placeholder"></div>
<script src="https://www.youtube.com/iframe_api"></script>
When the API is fully loaded, it looks for a global function called onYouTubeIframeAPIReady() which you should define.
Your code should look something like this
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('video-placeholder', {
width: 600,
height: 400,
videoId: 'Xa0Q0J5tOP0',
playerVars: {
color: 'white',
playlist: 'taJ60kskkns,FG0fTKAqZ5g'
},
events: {
onReady: initialize
}
});
}
Just make two buttons and call the needed method on click.
$('#play').on('click', function () {
player.playVideo();
});
$('#pause').on('click', function () {
player.pauseVideo();
});
You can use JavascriptInterface to get the callback inside the native java/kotlin code from WebView.
More details info
Youtube javascript player API with example
JavaScript Interface with example
I am using jquery to show my videos along wither other contents and the admob. It should apply to your case too as you are using webview.
You need to detect the event - onPlayerStateChange. Check this question which shows the code and prerequisties:
Check if YouTube video is playing and run script
why don't you use default videoview to play youtube videos it'll make easier communication between admob bannner and played video apply same logic as mentioned by kasiopeous

Disabling the youtube icon on the YouTubeStandalonePlayer in android?

I just wanted to know how to disable the youtube icon that comes in the YouTubeStandalonePlayer. Is it possible?
I don't want to take the user to youtube on clicking the icon. Is there any way to disable the youtube icon?
The below is the code how I am playing the the video using the YouTubeStandalonePlayer.
String videoId = getVideoId(cardsInfo.getCard_video_url());
Intent intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), getResources().getString(R.string.android_key), videoId, 0, true, false);
startActivity(intent);
According to the API, you can't.
But, you can try and use the other (not standalone) classes, which might give you better control as suggested in the docs: There are two ways to play videos. The first option is to place a YouTubePlayerFragment or YouTubePlayerView in your View hierarchy and then use the YouTubePlayer to control video playback in the View. **This gives a fine control of the experience**. If they don't let it, you could atleast have better control of the flow/UI-visibility because you are controling the Activity (in contrast of giving the control to the standalone activity)

Rewind functionality in Brightcove player

In my app I implement the brightcove video player.
I am working on customizing brightcove.
I successfully implemented the play/pause option, and I am trying the same for the rewind option, but to no avail.
Here is my code
BrightcoveVideoView commonVideoView = (BrightcoveVideoView)view.findViewById(R.id.commonVideoViewId);
BrightcoveMediaController mediaController = new BrightcoveMediaController(commonVideoView, R.layout.media_controllers);
How can I solve this issue?
You can use the seekTo method for achieving the same.
brightcoveVideoView.seekTo(value);

How to hide youtube logo in youtube player in android

I don't want to share youtube videos in my android application.But when i click youtube logo in youtube playe, the link goes to youtube website.
So, How can i hide youtube logo in youtube player.Please suggest some ideas...
Think is i don't want to share youtube videos
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 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"
Refer below links:
1] https://gist.github.com/TheFinestArtist/5545437
2]https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer.PlayerStyle#Enums
Try this way:
ViewGroup tmpVg = youTubeView;
int[] loopLv = new int[]{0,0,4,0,0,3,0,1};
for( int i = 0; i < loopLv.length; i++){
if( i == loopLv.length-1 ){
tmpVg.getChildAt(loopLv[i]).setVisibility( View.GONE );
} else {
tmpVg = (ViewGroup)tmpVg.getChildAt( loopLv[i] );
}
}
your problem will be solved.

Android media controller

By default the mediacontroller for video view appears only for 3 sec. I want it to be there untill the song is playing. I know I have to use this peice of code: -
mediacontrollername.show(0);
But apparently it doesn't seems to be doing its job correctly
What should I do for this?
Just in case anyone searched for this issue and still needs a solution, have a look at this: MediaController 3 Second Issue

Categories

Resources