YOUTUBE PLAYER FLUTTER

pub package Build Status licence Download Stars Top Language effective dart

用于使用官方 iFrame 播放器 API 内联播放或流式传输 YouTube 视频的 Flutter 插件。

支持的平台

  • Android
  • iOS 对于 Web 支持,请使用 youtube_player_iframe。将来,此包将扩展 youtube_player_iframe。

DEMO

主要特点

  • 内嵌播放
  • 支持字幕
  • 无需 API 密钥
  • 支持自定义控件
  • 检索视频元数据
  • 支持直播视频
  • 支持更改播放速率
  • 支持 Android 和 iOS
  • 根据带宽调整质量
  • 通过水平拖动快进和快退
  • 使用捏合手势将视频适配到宽屏

该插件在底层使用了 flutter_inappwebview

由于 flutter_inappwebview 依赖于 Flutter 嵌入 Android 和 iOS 视图的机制,因此此插件可能与标记为 platform-views 的一些已知问题共享。

要求

  • Android: minSdkVersion 17 并添加对 androidx 的支持(请参阅 AndroidX 迁移
  • iOS: --ios-language swift, Xcode 版本 >= 11

设置

iOS

无需配置

更多信息,请参见此处

Android

android/app/build.gradle 文件中的 minSdkVersion 设置为至少 17。

更多信息,请参见此处

注意:虽然最低设置为 17,但在 API < 20 的设备上(如果启用了混合组合,则为 19)播放器将无法播放。对于 API < 20 的设备,您可能希望使用 YouTube 应用转发要播放的视频,使用 url_launcherandroid_intent 等包。

使用 Youtube 播放器

YoutubePlayerController _controller = YoutubePlayerController(
    initialVideoId: 'iLnmTe5Q2Qw',
    flags: YoutubePlayerFlags(
        autoPlay: true,
        mute: true,
    ),
);

YoutubePlayer(
    controller: _controller,
    showVideoProgressIndicator: true,
    videoProgressIndicatorColor: Colors.amber,
    progressColors: ProgressColors(
        playedColor: Colors.amber,
        handleColor: Colors.amberAccent,
    ),
    onReady () {
        _controller.addListener(listener);
    },
),

全屏支持

如果需要全屏支持,请将您的播放器包装在 YoutubePlayerBuilder

YoutubePlayerBuilder(
    player: YoutubePlayer(
        controller: _controller,
    ),
    builder: (context, player){
        return Column(
            children: [
                // some widgets
                player,
                //some other widgets
            ],
        );
    ),
),

播放直播视频

将 isLive 属性设置为 true 以更改 UI 以匹配直播视频。

Live UI Demo

YoutubePlayerController _controller = YoutubePlayerController(
    initialVideoId: 'iLnmTe5Q2Qw',
    flags: YoutubePLayerFlags(
      isLive: true,
    ),
);

YoutubePlayer(
    controller: _controller,
    liveUIColor: Colors.amber,
),

想自定义播放器?

从 v5.x.x 开始,使用 topActionsbottomActions 属性来自定义播放器。

插件附带的一些小部件是

  • 全屏按钮
  • 剩余时长
  • 当前位置
  • 播放/暂停按钮
  • 播放速度按钮
  • ProgressBar

YoutubePlayer(
    controller: _controller,
    bottomActions: [
      CurrentPosition(),
      ProgressBar(isExpanded: true),
      TotalDuration(),
    ],
),

想使用 YouTube 网址播放吗?

该插件还提供了 convertUrlToId() 方法,该方法将 YouTube 链接转换为其对应的视频 ID。

String videoId;
videoId = YoutubePlayer.convertUrlToId("https://www.youtube.com/watch?v=BBAyRBTfsOU");
print(videoId); // BBAyRBTfsOU

示例

详细示例

快速链接

下载

从上面的(徽章内)下载 APK 并尝试该插件。APK 在 GitHub 发布页面的 Assets 中提供。

限制

由于该插件基于平台视图。此插件需要 Android API 级别 20 或更高版本。

许可证

Copyright 2021 Sarbagya Dhaubanjar. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

GitHub

查看 Github