我目前正在加載-UIwebView
內的YouTube視頻,此操作完美並且視頻自動播放。但問題是,一旦視頻在-UIwebView
內完成,它就會停止並顯示播放按鈕。不過,我希望視頻播放器關閉,因此用戶不必每次都手動點擊「完成」按鈕。完成播放後YouTube視頻不會關閉
Image of the screen when the video doesn't close.
這裏的鏈接我啓動視頻-UIwebview
NSString *youTubeVideoHTML = [NSString stringWithFormat:@"<!DOCTYPE html><html><head>
<style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script>
var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
width:'320',
height:'200',
videoId:'%@',
events: {
'onReady': onPlayerReady,
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
</script> </body> </html>", [[NSUserDefaults standardUserDefaults]
objectForKey:@"detailVideoURL"]];
編輯裏面:
我試圖實施第一個答案進入我的代碼,這是它是如何結束喜歡。我仍然不確定我應該在回調中添加什麼內容?
NSString *youTubeVideoHTML = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script>
var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
width:'320',
height:'200',
videoId:'%@',
events: {
'onReady': onPlayerReady,
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000); done = true;
}
if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:nil"; }; }
function stopVideo() {
player.stopVideo();
}
</script> </body> </html>", [[NSUserDefaults standardUserDefaults] objectForKey:@"detailVideoURL"];
圖像錯誤:http://gyazo.com/31da955d8af98f40b82789a7f60c1edb
你可以看一下我的編輯好嗎?它工作不正常,我不確定爲什麼先生。 –
@ GretarAgnarsson ..已完全編輯答案..請仔細閱讀.. –