【问题标题】:Embedding a Youtube video in Flutter Web在 Flutter Web 中嵌入 Youtube 视频
【发布时间】:2020-05-07 01:57:05
【问题描述】:

是否可以在 Flutter 网页中嵌入 Youtube 视频?我尝试使用以下代码将 Youtube 嵌入到我的 Flutter 网站中,但页面上没有出现任何错误消息。

import 'package:flutter_html_view/flutter_html_view.dart';

Container(child: HtmlView(data: """
          <iframe src="https://www.youtube.com/embed/xMzkWfIR9Pk" width="560" height="315"></iframe>
          """)),

还尝试了此处推荐的解决方案 (WebView in Flutter Web),但这也不起作用。

import 'dart:ui' as ui

//ignore: undefined_prefixed_name
  ui.platformViewRegistry.registerViewFactory(
      'hello-world-html',
      (int viewId) => html.IFrameElement()
        ..width = '640'
        ..height = '360'
        ..src = 'https://www.youtube.com/embed/IyFZznAk69U'
        ..style.border = 'none');

【问题讨论】:

标签: flutter youtube flutter-web


【解决方案1】:

我以前打包叫flutter_html 但这给了我一些滚动问题。

Html(
    data: '<iframe width="560" height="315" src="https://www.youtube.com/embed/D_mCZlQZg9Y" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
)

【讨论】:

    【解决方案2】:

    你应该去this

      import 'dart:html' as html;
      import 'dart:js' as js;
      import 'dart:ui' as ui;
    
      String viewID = "your-view-id";
    
      @override
      Widget build(BuildContext context) {
        // ignore: undefined_prefixed_name
        ui.platformViewRegistry.registerViewFactory(
            viewID,
                (int id) => html.IFrameElement()
              ..width = MediaQuery.of(context).size.width.toString()
              ..height = MediaQuery.of(context).size.height.toString()
              ..src = 'https://www.youtube.com/embed/IyFZznAk69U'
              ..style.border = 'none');
    
        return SizedBox(
          height: 500,
          child: HtmlElementView(
            viewType: viewID,
          ),
        );
      }
    

    【讨论】:

    • ui.platformViewRegistry有错误:尝试更正前缀或导入定义'platformViewRegistry'的库.dartundefined_prefixed_name
    • 名称“platformViewRegistry”是通过前缀“ui”引用的,但在使用该前缀导入的任何库中都没有定义。
    【解决方案3】:

    ext_video_player 支持 Android、iOS 和 YouTube 网页版。您可以尝试从 ext_video_player repo 运行示例代码。

    我使用不同操作系统的不同浏览器进行了测试。 Android、Mac、Windows 浏览器与 ext_video_player 一起工作正常。但它不适用于 iOS 浏览器。

    【讨论】:

    • 我尝试使用您指定的存储库。在网络上不起作用。它只是停留在线性进度小部件上,而不是加载 vid。
    【解决方案4】:

    至少有两种解决方案:

    a) video_player 包现在可以在三个主要平台上正常运行:android、ios 和 web

    b) 本文介绍了一种将视频源嵌入到 Flutter Web 小部件中的好方法,即使该文章是针对本地文件的,最终过程也适用于 youtube 等 Web 源:https://vermahitesh.medium.com/play-local-video-on-flutter-web-d757bab0141c

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2021-12-04
      • 2012-06-25
      • 1970-01-01
      • 2011-08-10
      • 2011-02-01
      • 2012-01-06
      • 2016-07-25
      相关资源
      最近更新 更多