Reputation: 7133
Relevant part of the activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_item);
loadTopico();
}
private void loadTopico() {
YouTubePlayerFragment mainVideo = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.main_video);
mainVideo.initialize(Config.YOUTUBE_API, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVide(getResources().getString(R.string.principal_funcoes));
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
YouTubePlayerFragment secondVideo = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.secondVideo);
secondVideo .initialize(Config.YOUTUBE_API, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(getResources().getString(R.string.secundario));
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
The intended result was to have the two fragments have a video each, and also be independent from each other.
The problem I'm facing is that the fragments don't have a video each, nor they're independent. Both load together, and everything done in one of the fragments (pressing play or pause) completely changes what happens in the other fragment.
The planned feature for this activity is show a feed of videos for the user to press play and watch each video individually, but as stated, that's not possible due to the fragments completely destroying each other.
What can I possibly do for the problem to be solved?
//EDIT R.layout.activity_sub_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kerooker.me.matematicando.SubItem"
android:id="@+id/mainRelative">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_below="@+id/textoAssista"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:id="@+id/main_video" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/texto_assista"
android:id="@+id/textoAssista"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_below="@+id/main_video"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:id="@+id/secondary_video" />
</RelativeLayout>
Upvotes: 0
Views: 531
Reputation: 6704
Both of your videos are currently loading with this youTubePlayer
instance regardless of Fragments. There is a YouTubePlayer instance that you can use through onInitializationSuccess method of each YouTubePlayer.OnInitializedListener
.
I'm changing your loadTopico method where you load one YouTubeFragment. Change the second fragment accordingly.
private void loadTopico() {
YouTubePlayerFragment mainVideo = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.main_video);
mainVideo.initialize(Config.YOUTUBE_API, new YouTubePlayer.OnInitializedListener() {
@Override
public abstract void onInitializationSuccess (YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored){
player.cueVideo(getResources().getString(R.string.principal_funcoes));
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
Update
Okay, so, I've done some digging and apparently you can't initialize multiple instances of YouTubePlayerFragment since its using a singleton pattern under the hood which basically means if you're to instantiate multiple YouTubePlayerFragment objects, you'll just end up having the same instance twice. Thus the reason you see the same behavior throughout the all instances.
The workaround is not to instantiate multiple instances of YouTubePlayerFragment all at once. Rather, initialize them only when you need them.
See this and this to find out more about this.
Upvotes: 2