我编写的代码在我的浏览器中运行良好:
<!DOCTYPE html>
<html style="height: 100%; width: 100%; margin: 0px">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="height: 100%; width: 100%; margin: 0px">
<div id="videoContainer">
</div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('videoContainer', {
height: `${document.body.scrollHeight}`,
width: `${document.body.scrollWidth}`,
videoId: 'p_9XvHBb3nw',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
console.log('playing');
}
else if (event.data == YT.PlayerState.PAUSED) {
console.log('paused');
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
但是,当我将其添加到我的应用程序并加载到我的 WebView
什么也没发生:
没有错误或任何警告。我甚至在控制台中复制并粘贴了脚本,但没有被触发或任何东西。另外,我检查后知道高度不是0。
这是我加载它的方式:
var webView = FindViewById<WebView>(Resource.Id.webView);
webView.Settings.JavaScriptEnabled = true;
webView.Settings.AllowFileAccessFromFileURLs = true;
webView.Settings.DomStorageEnabled = true;
webView.Settings.LoadWithOverviewMode = true;
webView.Settings.AllowFileAccess = true;
webView.Settings.AllowUniversalAccessFromFileURLs = true;
webView.Settings.AllowContentAccess = true;
webView.Settings.SetPluginState(WebSettings.PluginState.On);
webView.SetWebChromeClient(new WebChromeClient());
webView.LoadUrl("file:///android_asset/website/video.html");
知道我做错了什么吗?
附笔。:
我想强调我不想使用youtube api。并非所有我的用户都安装了youtube应用程序,也不想强迫他们使用它:
注意:用户需要运行移动youtube应用程序的4.2.16版(或更高版本)才能使用api。
资料来源:https://developers.google.com/youtube/android/player
暂无答案!
目前还没有任何答案,快来回答吧!