嵌入式 Youtube 不会在 UIWebView 中自动播放视频
我正在尝试将 YouTube 视频嵌入 UIWebView 并使其自动播放。显然这不起作用。这是否与以下内容相关:
还是某种错误?
这是我的 html 文件:
<html>
<head>
<script>
document.ontouchmove=function(e){ e.preventDefault(); };
</script>
</head>
<body style="margin:0px;" bgcolor="#000000">
<!-- 1. The <div> tag will contain the <iframe> (and video player) -->
<div id="player" style="text-align:center;"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_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 onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '400',
width: '768',
videoId: 'HYlD0KXujAk',
playerVars: {'autoplay' : 1},
events: {
'onStateChange': onPlayerStateChange
}
});
}
// 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.
var done = false;
function onPlayerStateChange(event) {
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
I am trying to embed a youtube video in a UIWebView and have it autoplay. Apparently this does not work. Is this related to:
or is it some kind of bug?
Here is my html file:
<html>
<head>
<script>
document.ontouchmove=function(e){ e.preventDefault(); };
</script>
</head>
<body style="margin:0px;" bgcolor="#000000">
<!-- 1. The <div> tag will contain the <iframe> (and video player) -->
<div id="player" style="text-align:center;"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_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 onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '400',
width: '768',
videoId: 'HYlD0KXujAk',
playerVars: {'autoplay' : 1},
events: {
'onStateChange': onPlayerStateChange
}
});
}
// 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.
var done = false;
function onPlayerStateChange(event) {
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不是错误,这是默认行为。
UIWebView
在没有用户干预的情况下不会自动播放任何内容,除非您将UIWebView
的mediaPlaybackRequiresUserAction
属性设置为NO
(默认情况下是YES
)。然后它就会起作用。No this is no bug, this is default behavior.
UIWebView
will not play anything automatically without user intervention, except if you setUIWebView
'smediaPlaybackRequiresUserAction
property toNO
(it'sYES
by default) yourself. Then it will work.