【发布时间】: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