Flutter - 如何在返回事件后关闭webview中的youtube视频?

问题描述 投票:-1回答:2

所以我只是实现了这个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
2个回答
0
投票

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


0
投票

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

要停止来自youtube或任何其他有音频的网站的音频,我们需要更改当前webview的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,
              ),
            )
          ],
        ),
      ),
    );
© www.soinside.com 2019 - 2024. All rights reserved.