我正在尝试在Android中以自动播放方式播放youtubeVideo,当我在WebView中使用以下代码时,该视频无法运行,但是当我添加 ws.setMediaPlaybackRequiresUserGesture(false); 时,它将运行自动播放youtubeVideo但是我想在html上运行该怎么办?我正在android,youtube和其他小部件中运行小部件,我必须以html格式自动播放YouTube视频。
public final class Config {
private Config(){
}
public static final String YOUTUBE_API_KEY = "AIzaSyA7rAQQLWq-ybR3VgfxdBzIgDQ56fGPA5c";
public static final String HTML = "<!DOCTYPE html>\n" +
"<html>\n" +
" <style type=\"text/css\">\n" +
" html, body {\n" +
" height: 100%;\n" +
" width: 100%;\n" +
" margin: 0;\n" +
" padding: 0;\n" +
" background-color: #000000;\n" +
" overflow: hidden;\n" +
" position: fixed;\n" +
" }\n" +
" </style>\n" +
"<body>\n" +
"\n" +
" <div id=\"player\"></div>\n" +
"<script>\n" +
" var tag = document.createElement('script');\n" +
" tag.src = \"https://www.youtube.com/player_api\";\n" +
" var firstScriptTag = document.getElementsByTagName('script')[0];\n" +
" firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n" +
" var player;\n" +
" function onYouTubePlayerAPIReady() {\n" +
" player = new YT.Player('player', {\n" +
" height: '100%',\n" +
" width: '100%',\n" +
" videoId: 'CUSTOM_ID',\n" +
" events: {\n" +
" 'onReady': onPlayerReady,\n" +
" 'onStateChange': onPlayerStateChange\n" +
" },\n" +
" playerVars: {\n" +
" 'autoplay': 0,\n" +
" 'showinfo': 0,\n" +
" 'controls': 0\n" +
" }\n" +
" });\n" +
" }\n" +
" function onPlayerReady(event) {\n" +
" console.log('OnPleyer Ready') \n"+
" event.target.playVideo();\n" +
"\n" +
" }\n" +
"\n" +
" var done = false;\n" +
" function onPlayerStateChange(event) {\n" +
" if (event.data == YT.PlayerState.PLAYING && !done) {\n" +
" console.log('OnStateChanged Ready') \n"+
" done = true;\n" +
" }\n" +
" }\n" +
" function stopVideo() {\n" +
" player.stopVideo();\n" +
" }\n" +
" </script> \n" +
"\n" +
"</body>\n" +
"</html>";
}
活动
webViewYoutube.loadDataWithBaseURL("https://www.youtube.com/", Config.HTML.replace("CUSTOM_ID", url), "text/html", "utf-8", null);
webLytYoutube.setVisibility(View.VISIBLE);