【问题标题】:Display several youtube video in an activity在一个活动中显示多个 youtube 视频
【发布时间】:2017-06-29 13:03:11
【问题描述】:

我有一个我想在我的活动中显示的文章对象。一篇文章可以有: -文本:至少 1 个或多个 - 图像:0 个或多个 - 视频:0 个或多个

每个元素都可以在文章中的任何位置,例如: -文本/图像/文本/视频 -视频/图像/视频/文本 - 等等...

解析我的文章后,我以编程方式添加我需要的内容: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>

稍后我想以编程方式在我的 XML 中为文章对象中找到的每个视频添加片段标记。

实际上,我的活动中有 2 个播放器,但只有一个正在播放视频,每个播放器中的操作(播放/暂停/全屏)都会在 2 个播放器中执行相同的操作。

【问题讨论】:

标签: android android-fragments android-linearlayout android-youtube-api appcompatactivity


【解决方案1】:

首先你必须通过 onInitializationSuccess() 中的 id 区分两个提供者。

如果提供者是frag1,调用你TubePlayer.loadVideo("g2mmUzNSeDM") 否则,如果提供者是 frag2,则调用 youTubePlayer.loadVideo("nCgQDjiotG0")

(如果您的活动扩展了 YoutubeBaseActivity,您可以通过编程方式将 YouTubePlayerView 添加到您的布局中)

【讨论】:

  • 在进入onInitializationSuccess之前如何知道frag1提供者的id是什么?如果我看到特定的 Providersid,我需要知道我必须加载哪个 src 实际上我不能使用 YoutubePlayerView,因为我扩展了 appCompatActivity,所以我读到使用 YoutubePlayerFragment 是解决方案。
  • Id 就是一个例子。您可以轻松比较。 if (frag1.equals(provider)) { // TODO: 调用 loadVideo } else if (frag2.equals(provider)) { // TODO: 调用 loadVideo }
  • 它不会改变任何东西,即使在这种情况下,唯一播放的视频是最后加载的......
猜你喜欢
  • 2013-06-11
  • 1970-01-01
  • 2014-07-15
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多