2016-10-12 7 views
0

Ich versuche, YouTube Videos innerhalb eines vorhandenen Fragments auf Laufzeit mit anderen Fragment zu spielen. Ich versuche, dem Fragment in einem anderen Fragment zu folgen. Aber ich kann keine Videos sehen, nur Audio spielt mit der vorherigen Fragmentansicht. Bitte hilf mir. Ich habe ungefähr 200 Seiten abgefragt, aber nichts hat für mich funktioniert. Jede Hilfe wird geschätzt.Wie youtube Video auf anderen Fragment in Android spielen

Hier ist mein my_fragment.xml Code:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/fragmentOpponents" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white" 
android:keepScreenOn="true"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/opponents" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:numColumns="3" /> 

<FrameLayout 
    android:id="@+id/child_fragment" 
    android:layout_width="250dp" 
    android:layout_height="250dp"> 
</FrameLayout> 
</RelativeLayout> 

-Code für YoutubeFragment:

public class YoutubeFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener { 

private static final int RECOVERY_DIALOG_REQUEST = 1; 

private static final String KEY_VIDEO_ID = "KEY_VIDEO_ID"; 

private String mVideoId; 

//Empty constructor 
public YoutubeFragment() { 
} 

/** 
* Returns a new instance of this Fragment 
* 
* @param videoId The ID of the video to play 
*/ 
public static YoutubeFragment newInstance(final String videoId) { 
    final YoutubeFragment youTubeFragment = new YoutubeFragment(); 
    final Bundle bundle = new Bundle(); 
    bundle.putString(KEY_VIDEO_ID, videoId); 
    youTubeFragment.setArguments(bundle); 
    return youTubeFragment; 
} 

@Override 
public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 

    final Bundle arguments = getArguments(); 

    if (bundle != null && bundle.containsKey(KEY_VIDEO_ID)) { 
     mVideoId = bundle.getString(KEY_VIDEO_ID); 
    } else if (arguments != null && arguments.containsKey(KEY_VIDEO_ID)) { 
     mVideoId = arguments.getString(KEY_VIDEO_ID); 
    } 

    initialize(Consts.DEVELOPER_KEY, this); 
} 

@Override 
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) { 

    if (mVideoId != null) { 
     if (restored) { 
      youTubePlayer.play(); 
     } else { 
      youTubePlayer.loadVideo(mVideoId); 
     } 
    } 
} 

@Override 
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 
    if (youTubeInitializationResult.isUserRecoverableError()) { 
     youTubeInitializationResult.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show(); 
    } else { 
     //Handle the failure 
     Toast.makeText(getActivity(), R.string.error_init_failure, Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle bundle) { 
    super.onSaveInstanceState(bundle); 
    bundle.putString(KEY_VIDEO_ID, mVideoId); 
    } 
} 

ich diesen Code bin mit einem Fragment zur Laufzeit nennen,

FragmentManager fragmentManager = getSupportFragmentManager(); 
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
        Fragment fragment = YoutubeFragment.newInstance("q8_o4W9ZyZk"); 
        fragmentTransaction.add(R.id.child_fragment, fragment); 
        fragmentTransaction.addToBackStack(null); 
        fragmentTransaction.commit(); 
+0

Hallo, wo testen Sie? Im Emulator oder echten Gerät? Welche Versionen von Android testen Sie? – jos

+0

Auf einem echten Gerät. Android-Version 6.0.1 (Marshmallo) – Archana

Antwort

0

Wie Sie usi sind ng verschachtelte Fragmente, verwenden getChildFragmentManger() Methode anstelle getSupportFragmentManager

Here und here gibt es weitere Informationen

Hoffe, es hilft!

+0

Hat nicht funktioniert. :-( – Archana

+0

Und welche Klasse tust du das? In deinem Fragment? – jos

+0

Innerhalb einer Aktivität rufe ich youtube fragment an. – Archana