Reputation: 356
I'm wanting to add a button to the YouTubePlayerView but don't know the best way to go about doing so. I would like the button only to be present when the user clicks on the screen to bring up the youtube controls (play/pause, timeline, share, etc.) that fades away after a few seconds just as the controls fade. I've looked into ViewGroupOverlay but not sure if this is the best way to do so and I'm not sure how to only have the button added to the overlay when the controls are available. It feels like I have to override one of the YouTubePlayer or YouTubePlayerView functions to know when the controls are visible but I'm just making a hypothesis here. I'm relatively new to android and it's been a while since I've done anything serious in Java. Any direction here would be great if others don't know the exact answer. I've looked around for about 2 hours before posting this :)
package com.example.something.exampletube;
import android.os.Bundle;
import android.view.ViewGroupOverlay;
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.YouTubePlayerView;
/**
* Created by Nicholas Dwyer on 4/17/2015.
* This class is used to play youtube videos
*/
public class PlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
YouTubePlayerView playerView;
ViewGroupOverlay groupOverlay;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_player);
playerView = (YouTubePlayerView) findViewById(R.id.player_view);
playerView.initialize(YoutubeConnector.KEY, this);
groupOverlay = playerView.getOverlay();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) {
if(!restored) {
youTubePlayer.cueVideo(getIntent().getStringExtra("VIDEO_ID"));
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
String errorMessage = "There was an error initializing the YouTubePlayer" + youTubeInitializationResult.toString();
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
//Toast.makeText(this,getString(R.string.failed), Toast.LENGTH_LONG).show();
}
}
Upvotes: 4
Views: 2628
Reputation: 440
Instead of adding button as a layer above the player, you can add it INSIDE the player:
Button b = new Button(this);
b.setBackgroundResource(R.drawable.close);
((ViewGroup)((ViewGroup) youTubePlayerView.getChildAt(0)).getChildAt(0)).addView(b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
Working for me.
Upvotes: 0
Reputation: 709
Another good alternative is to used Exoplayer, to overlay your video with view. It is not part of the android sdk, but it's recommended by google and included in android developer documentation :
http://google.github.io/ExoPlayer/ https://developer.android.com/guide/topics/media/exoplayer.html
Exoplayer allow you to stream any kind of video, not only Youtubes videos.
It's also good to mention that Exoplayer is used in Youtube application.
Upvotes: 0
Reputation: 4987
It is not possible to add buttons as overlays above the player as specified by Google:
Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay the view with other views while a video is playing.
See also here Is it possible to overlay some views on YouTube Android Player?
If any view only partially overlaps (or YouTubeVideoView is only partially scrolled outside the visible screen), the video is stopped after approximately one second. Google supposedly does that to prevent abuse of videos for advertising outside YouTube.
Alternatives: Use a WebView and embed it.
Upvotes: 3