本文介绍了Flutter-发生后退事件后如何在Webview中关闭youtube视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是短暂地实现了此WebView,这很棒,但是当我使用webview嵌入youtube视频时出现了一个问题,即使关闭Webview页面,视频仍然可以播放。我如何关闭它?

So i just implemented this WebView in flutter and it's great, but there's a problem when i embedd a youtube video using webview, the video is still playing even i close the Webview page. How do i turn it off?

我使用的插件是 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');
},
)


推荐答案

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

I finally get the answer but i change the package to webview_flutter instead of flutter_webview_plugin.

要停止youtube或其他有音频网站的音频,我们需要更改<$ c当前网络视图的$ c> url 。也许它也可以与 flutter_webview_plugin 一起使用。

To stop the audio from youtube or any other website that has audio we need to change the url of the current webview. and maybe it will work with flutter_webview_plugin too.

/* 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,
              ),
            )
          ],
        ),
      ),
    );

这篇关于Flutter-发生后退事件后如何在Webview中关闭youtube视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 04:09