我正在使用Flutter的 Webview
为加载我的 youtube网址 而我想要的是在webview加载后自动播放该视频。我使用的是一个 .html file
放在 assets
文件夹,用于从本地资产文件加载url。
下面是我的代码
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 但它不工作在我的情况下。
翩翩官方Webview有很多问题,有些时候这个库在一些js函数中不工作,使应用程序崩溃。而且这个库还 开发预览. 但是... flutter_webview_plugin 库是非常有用的,而且工作非常稳定。顺便说一下:我用的是flutter_webview_plugin,我从官方库换成了flutter_webview_plugin。