I have a dialogue that I want to show a YouTube video, I charge it in the following way
public void video(final String video) {
final Dialog dialog = new Dialog(Leyes.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.youtube_video);
dialog.setCancelable(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
youTubePlayerView = (YouTubePlayerView) dialog.findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(getResources().getString(R.string.youtube_key), new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
ytp=youTubePlayer;
ytp.loadVideo(video);
ytp.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {
}
@Override
public void onLoaded(String s) {
}
@Override
public void onAdStarted() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onVideoEnded() {
dialog.dismiss();
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
TastyToast.makeText(getApplicationContext(),"Error al cargar el video",TastyToast.LENGTH_LONG,TastyToast.ERROR).show();
dialog.dismiss();
}
});
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
TastyToast.makeText(getApplicationContext(),"Error al cargar el video",TastyToast.LENGTH_LONG,TastyToast.ERROR).show();
dialog.dismiss();
}
});
dialog.show();
dialog.getWindow().setAttributes(lp);
}
gives me the following error:
Caused by: java.lang.IllegalStateException: To YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
being that the class of my activity is defined as follows:
public class Laws extends YouTubeBaseActivity implements AppCompatCallback {
I hope you can help me