I have an application to randomly play YouTube videos using the YouTube API, to get a random video id and play the video I have a button called nextVideo, but when I press it it does not allow the methods of youtubePlayer to be invoked:
public void siguienteVideo(View v){
youTubePlayer.loadVideo(videoId);
youTubePlayer.play();
}
Nor use public api methods like:
public void cueVideo (String videoId){
youTubePlayer.loadVideo(videoId);
youTubePlayer.play();
}
public void siguienteVideo(View v){
cueVideo(idVideoFinal);
}
I get this exception:
java.lang.IllegalStateException: Could not execute method for android:onClick
Initialization:
//Método para comprobar si fue bien
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
//Si fue bien entonces carga y reproduce el video
if(!b){
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
//Carga y reproduce directamente el video
youTubePlayer.loadVideo("DRS_PpOrUZ4");
youTubePlayer.play();
}
}
//Método para comprobar si algo fue mal
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
//Si existió algún error al inicializar muestra un dialog con el error
if(youTubeInitializationResult.isUserRecoverableError()){
youTubeInitializationResult.getErrorDialog(this, 1).show();
}
//Si YT no sabe cual es el error enviará este mensaje
else{
Toast.makeText(getApplicationContext(), "Error al inializar YouTube "+youTubeInitializationResult.toString(), Toast.LENGTH_LONG).show();
}
}
The youtubePlayer that doesn't work for me is the one with the next buttonVideo(View v)
SOLUTION
I have found a solution to the problem:
As a global variable I have a variable of type YoutubePlayer
private YouTubePlayer youTubePlayer1;
And when initializing:
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
//Si fue bien entonces carga y reproduce el video
if(!b){
youTubePlayer1=youTubePlayer;
youTubePlayer1.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
//Carga y reproduce directamente el video
youTubePlayer1.loadVideo("DRS_PpOrUZ4");
youTubePlayer1.play();
}
}
And so it can be used anywhere:
public void siguienteVideo(View v){
youTubePlayer1.loadVideo("ZsUMwahBvh8");
youTubePlayer1.play();
}
If you call the methods from your layout using
android:onClick
you must define the name of the method:and in code define the method, it must receive a view as a parameter
(View v)
:Regarding the problem:
this happens if you successfully call your method but this method has an error executing. In this case the error is:
and it is happening because the variable
youTubePlayer
is not initialized in this line of code:YouTube Android API
To use this API, initialization can be done in
onCreate()
and onInitializationSuccess() ensures that it starts correctly, and is where you can play your video.It's important to know that your class must extend YouTubeBaseActivity :
Implementation example: