有什么办法可以同时在一个屏幕上播放两个 youtube 视频吗?

Is there any way to play two youtube videos in one screen at the same time?

我有一个 activity 并想在其中嵌入两个 YouTube 播放器并允许用户同时播放。

我需要这个功能来允许用户比较两个视频。

但是,我无法做到这一点。当我在一个视频中按下播放按钮时,第二个视频停止了。

我已经尝试在布局中使用 Activity 和两个 youtube 片段,activity 在布局中使用两个 youtube 播放器,activity 在布局中使用两个 webview,其中我使用 youtube iFrame,但我一直都有相同的结果 - 只有一个播放视频。

此外,我发现 youtube api 对一个视频有限制。有什么解决方法可以同时在一个屏幕上播放两个视频吗?

尝试同时创建 2 个片段 activity 并使用 YouTubePlayerSupportFragment ? 注意:我没有对此进行测试,但它可能会起作用。

有一个名为 youtubedoubler.com 的网站,为您提供可以同时观看 2 个视频的 URL。

是的,您不能 运行 YouTube 视图的多个实例

https://github.com/flipkart-incubator/inline-youtube-view。这是一套实用程序库,围绕在 Android、iOS 或 React Native 应用程序中使用 YouTube。

它支持在一个片段中播放多个内联 youtube 视频。

使用这个库 (Android-YouTube-Player) 同时播放多个 YouTube 视频应该相当简单。只需将更多 YouTubePlayerView 添加到您的 Activity/Fragment.

将两个 YouTubePlayerView 添加到您的布局 xml 文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerView
        android:id="@+id/youtube_player_view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerView
        android:id="@+id/youtube_player_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

然后在您的 Activity 中为两个 YouTubePlayerView 重复此代码:

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view1);

youTubePlayerView.initialize(new YouTubePlayerInitListener() {
    @Override
    public void onInitSuccess(final YouTubePlayer initializedYouTubePlayer) {    
        initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady() {
                String videoId = "6JYIGclVQdw";
                initializedYouTubePlayer.loadVideo(videoId, 0);
            }
        });        
    }
}, true);

您应该可以使用任意数量的 YouTubePlayerView 执行此操作。