0

我收到了一篇文章對象,我想在我的活動中顯示。一個物品可以具有: -Text(S):1至少或幾個 -Image(S):0或幾個 -Video(S):0或幾個在活動中顯示幾個YouTube視頻

每個元素可以是文章中的任何位置例如: - 文本/圖片/文字/視頻 - 視頻/圖像/視頻/文本 - 等等......

分析我的文章後,我加入編程我需要的內容:TextView的,ImageView的,或VideoView( youtube或jwplayer)。其實這也適用於圖像和文字,但我不知道我怎麼可以在同一個活動中添加多個YouTube視頻:

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.post); 

    linearLayout = (LinearLayout) findViewById(R.id.llayout_article); 

    // Get article to display it 
    Intent intent = getIntent(); 
    Article article = intent.getParcelableExtra("articleDetails"); 

    //add ImageView & TextView 
    for (View view : article.getContentSplitInViews(this)) { 
     this.linearLayout.addView(view); 
    } 


    YouTubePlayerSupportFragment frag = 
      (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_fragment); 
    src="g2mmUzNSeDM"; 
    frag.initialize(apiKey, this); 
    YouTubePlayerSupportFragment frag2 = 
      (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_fragment2); 
    src = "nCgQDjiotG0"; 
    frag2.initialize(apiKey, this); 
} 

@Override 
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { 
    if (!wasRestored) { 
     youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); 
     youTubePlayer.loadVideo(src); 
     youTubePlayer.play(); 
    } 
} 

@Override 
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 
    // YouTube error 
    String errorMessage = youTubeInitializationResult.toString(); 
    Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show(); 
    Log.d("errorMessage:", errorMessage); 
} 

我當前xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/scrollViewId" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <LinearLayout 
     android:id="@+id/llayout_article" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:orientation="vertical"> 

     <fragment 
      android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" 
      android:id="@+id/youtube_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 


     <fragment 
      android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" 
      android:id="@+id/youtube_fragment2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 
</ScrollView> 

後來我想programmaticaly添加文章對象中找到的每個視頻的XML中的片段標籤。

實際上,我的活動中有2名玩家,但只有一名玩家正在播放視頻,並且每個玩家的操作(播放/暫停/全屏)都會在2名玩家中做同樣的事情。

android youtube api

+0

https://androidtutorialmagic.wordpress.com/my-mini-project/multiple-youtube-video-in-recyclerviewlistview-in-android/ –

+0

不錯的教程,但我不真的想使用youtubeStandAlonePlayer。有沒有辦法顯示多個視頻,然後點擊「播放按鈕」播放正確的視頻? – gamerounet

回答

0

所有你必須從兩個供應商的ID在onInitializationSuccess區分的第一個()。

如果提供的是frag1,調用youTubePlayer.loadVideo( 「g2mmUzNSeDM」) 否則如果供應商是frag2,調用youTubePlayer.loadVideo( 「nCgQDjiotG0」)

(你可以以編程方式添加YouTubePlayerView到您的佈局如果你的活動延伸YoutubeBaseActivity)

+0

在進入onInitializationSuccess之前,如何知道frag1提供者的id是什麼?我需要知道哪些src我必須加載,如果我看到一個特定的Providersid 其實我不能使用YoutubePlayerView,因爲我擴展了appCompatActivity,所以我讀到使用YoutubePlayerFragment是解決方案。 – gamerounet

+0

身份證將是一個例子。你可以輕鬆地比較。 if(frag1.equals(provider)){// TODO:call loadVideo} else if(frag2.equals(provider)){// TODO:call loadVideo} – jkirtyan

+0

它不會改變任何東西,即使有這個條件,唯一的播放的視頻是最後加載的... – gamerounet