javascript-Youtube Api playVideo方法在某些移动设备上不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-Youtube Api playVideo方法在某些移动设备上不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2909字,纯文字阅读大概需要5分钟。
内容图文
我正在尝试创建一个用于移动设备的网站,在该网站上点击图片后,可以播放youtube视频.
我已经在几种Android手机/版本上进行了测试,但在某些行为上并未达到预期的效果.
我的意思是,它停止缓冲并且永远无法播放视频.我注意到的另一件事是,播放器在用户触发视频播放之后才开始播放,而不是通过编程播放.如果我直接向youtube播放器显示,用户会单击以播放视频,然后单击按钮/图像播放另一个视频,这将更有效.
我在这里发布了我使用过JsFiddle的测试页
$(document).ready(function () {
// Caching jQuery objects
var $body = $('body'),
$log = $('#log'),
$yt = $('#ytplayer'),
$ytwrap = $('#ytwrapper'),
$choices = $('#choices');
// This code loads the YouTube API
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
$body.append(tag);
// This will become the player object when the API is ready
var player;
// See what kind of device we're using
var userAgent = navigator.userAgent.toLowerCase();
var isAndroid = userAgent.indexOf('android') > -1;
var isIpad = userAgent.indexOf('ipad') > -1;
var isIphone = userAgent.indexOf('iphone') > -1;
window.onYouTubeIframeAPIReady = function onYouTubeIframeAPIReady() {
player = new YT.Player('ytplayer', {
videoId: videos[0],
playerVars: {
allowfullscreen: 'allowfullscreen',
playsinline: 0
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
window.player = player;
//hide player
slidePlayer(false);
};
function onPlayerStateChange(event) {
// When a video starts playing,
// enable the fake fullscreen mode on Android & iPad
if (event.data == YT.PlayerState.PLAYING) {
if (isIpad) {
fakeFullScreen(true);
} else if (isAndroid) {
fakeFullScreen(true);
}
}
// On pause: hide the player, show thumbs
if (event.data == YT.PlayerState.PAUSED) {
if (isAndroid) {
// Exit fullscreen
fakeFullScreen(false);
// Scroll back to choices
window.scrollTo(0, playerTop);
} else if (isIpad) {
fakeFullScreen(false);
window.scrollTo(0, playerTop);
} else if (isIphone) {
slide(false);
}
}
}
$('#vstImageAd .imageWrap img').click(function (e) {
e.preventDefault();
var $this = $(this);
if (player) {
$this.css("display", "none");
slidePlayer(true);
player.playVideo();
}
});
// When a thumb image is pushed, start the video
$('#choices .playthumb img').click(function (e) {
var $this = $(this),
nr = parseInt($this.data('nr'));
if (!videos[nr]) nr = 1;
player.loadVideoById(videos[nr]);
// Hide the thumbs
slide(true);
});
});
解决方法:
看起来您正在使用的某些功能(player.playVideo())在移动设备中已禁用.
以我为例,在某些Android设备上使用player.playVideo()后,即使点击播放器,视频也无法播放
https://developers.google.com/youtube/iframe_api_reference?hl=zh-TW#Mobile_considerations
Autoplay and Scripted Playback
The HTML5 element, in certain mobile browsers (such as Chrome
and Safari), only allows playback to take place if it’s initiated by a
user interaction (such as tapping on the player).Due to this restriction, functions and parameters such as autoplay,
playVideo(), loadVideoById() won’t work in all mobile environments**
内容总结
以上是互联网集市为您收集整理的javascript-Youtube Api playVideo方法在某些移动设备上不起作用全部内容,希望文章能够帮你解决javascript-Youtube Api playVideo方法在某些移动设备上不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。