I have this Activity (videomedia) and I was able to make it play one of the videos but I don't know how to import the second YouTube video, I have the library imported without problems and with a single video if the app works so far.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videomedia);
youTubePlayerView=(YouTubePlayerView)findViewById(R.id.youtube_view);
youTubePlayerView.initialize(claveYoutube,this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean fueRestaurado) {
if (!fueRestaurado){
youTubePlayer.cueVideo("N9SPr-xvLGc"); //https://www.youtube.com/watch?v=N9SPr-xvLGc
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()){
youTubeInitializationResult.getErrorDialog(this, 1).show();
}else {
String error = "Error al inicializar Youtube"+youTubeInitializationResult.toString();
Toast.makeText(getApplication(),error,Toast.LENGTH_LONG).show();
}
}
protected void onActivityResult(int requestcCode, int resultcode, Intent data){
if (resultcode==1){
getYoutubePlayerProvider().initialize(claveYoutube,this);
}
}
protected YouTubePlayer.Provider getYoutubePlayerProvider(){
return youTubePlayerView;
}
@Override
public void onPlaying() {
}
@Override
public void onPaused() {
}
@Override
public void onStopped() {
}
@Override
public void onBuffering(boolean b) {
}
@Override
public void onSeekTo(int i) {
}
}
//This is my xml:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".videomedia">
first video
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="141dp"/>
second video
<com.google.android.youtube.player.
android:id="@+id/youtube_view2"
android:layout_width="match_parent"
android:layout_height="141dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginStart="0dp"
android:layout_marginTop="266dp"/>
The videomedio.java could only be programmed to play the first video, but the second video, how could I do it since that part of the code should repeat the method. When I try to repeat it, the application closes.
When using the YoutubePlayer API there are certain restrictions that you must take into account:
A view should not be added above the player.
Only one player is allowed for playback within the same
Activity
, even if each player is in aFragment
different withinActivity
the playback is not allowed.