【问题标题】:Flutter - How to turn off youtube video in webview after back event?Flutter - 如何在返回事件后关闭 webview 中的 youtube 视频?
【发布时间】:2019-08-22 04:07:06
【问题描述】:

所以我刚刚在 Flutter 中实现了这个 WebView,它很棒,但是当我使用 webview 嵌入 youtube 视频时出现问题,即使我关闭 Webview 页面,视频仍在播放。怎么关掉?

我使用的插件是flutter_webview_plugin

final flutterWebviewPlugin =FlutterWebviewPlugin();

@override
  void initState() {
    super.initState();
    flutterWebviewPlugin.close();
  }

  @override
  void dispose() {
    super.dispose();
    flutterWebviewPlugin.dispose();
  }

这是小部件:

IconButton(
   icon: Icon(Icons.more_vert),
   onPressed: () {
   print('Hello there!'); flutterWebviewPlugin.launch('https://www.youtube.com/embed/m5rm8ac4Gsc');
},
)

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以做的是创建一条路线,但在其中您的 webview 示例:/youtubeWebview 并使用 Navigator.popAndPushNamed(context, '/yourRoute'); 返回而不是 Navigator.pop(context);

    【讨论】:

    • 对不起,但仍然无法正常工作,即使应用程序最小化,音频仍然存在,我什至尝试使用这个Navigator.of(context).pushNamedAndRemoveUntil('/', (Route<dynamic> route) => false) 而不是你建议的,但仍然没有运气。但感谢您的回复
    【解决方案2】:

    我终于得到了答案,但我将包更改为 webview_flutter 而不是 flutter_webview_plugin

    要停止来自 youtube 或任何其他有音频的网站的音频,我们需要更改当前 web 视图的 url。也许它也适用于flutter_webview_plugin

    /* define webview controller */
    WebViewController _controller;
    
    /* before we leave current route, make sure to change the url to something */
    Future<bool> _willPopCallback(WebViewController controller) async {
      controller.loadUrl('https://www.google.com/'); /* or you can use controller.reload() to just reload the page */
    
      return true;
    }
    
    return WillPopScope(
      onWillPop: () => _willPopCallback(_controller), /* call the function here */
      child: Scaffold(
         appBar: AppBar(
            title: Text('Just appbar'),
         ),
         body: Column(
            children: <Widget>[
               Expanded(
                  child: WebView(
                    key: UniqueKey(),
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: widget.videoUrl,
                    onWebViewCreated: (WebViewController webViewController) { /* i am not sure what this line actually do */
                      _controller = webViewController;
                    },
                  ),
                ),
                Text(
                  'Please pause the video before you go back',
                  style: TextStyle(
                    color: Colors.black,
                  ),
                )
              ],
            ),
          ),
        );
    

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多