【问题标题】:Youtube's autoplay video is not wroking on flutter webview for AndroidYoutube 的自动播放视频不适用于 Android 的颤振 webview
【发布时间】:2020-05-28 09:00:38
【问题描述】:

我正在使用 Flutter 的 Webview 加载我的 youtube url,我想要的是在 webview 加载后自动播放该视频。 我正在使用放在我的assets 文件夹中的.html file 来从本地资产文件加载网址。

下面是我的代码-

my_asset.html

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body,html{
    height:100%;
    margin:0;
    }
</style>
</head>
<body>
<div>
    <iframe width ="100%" height="100%" src="https://www.youtube.com/embed/uJCzdk4EFgw?autoplay=1" frameborder="0" allowfullscreen></iframe>
</div>

</body>
</html>

还有我的 webview.dart 文件 -

@override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: null,
      body:SafeArea(
        child: Stack(
          children: [
            Container(
              child: WebView(
                initialUrl:"about:blank",
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (WebViewController webViewController) {
                  _controller = webViewController;
                  _loadHtmlFromAssets();
                },
                ),
            ),
            Positioned(
              top: 10.0,
              right: 10.0,
              child: IconButton(
                icon: Image.asset(AppAssets.closeIcon),
                iconSize: 30.0,
                onPressed: (){
                  Navigator.pop(context);
                },
              ),
            ),


          ],
        ),
      ),
    );

    return Container();
  }

  _loadHtmlFromAssets() async {
    String fileText = await rootBundle.loadString(widget.asset);
    _controller.loadUrl( Uri.dataFromString(
        fileText,
        mimeType: 'text/html',
        encoding: Encoding.getByName('utf-8')
    ).toString());
  }

自动播放的关键是 ?autoplay=1,但在我的情况下它不起作用。

【问题讨论】:

    标签: flutter android-webview flutter-web youtube-iframe-api autoplay


    【解决方案1】:

    Flutter 官方 Webview 有很多问题,有时候这个库在一些 js 函数中不起作用,导致应用崩溃。而且这个库还在开发预览。但是flutter_webview_plugin 库非常有用并且工作非常稳定。顺便说一句:我用的是flutter_webview_plugin,我从官方库切换到flutter_webview_plugin

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 2013-07-13
      • 2014-02-10
      • 2012-12-14
      • 2015-03-18
      • 2021-08-04
      • 2020-03-02
      • 2012-06-07
      • 1970-01-01
      相关资源
      最近更新 更多