gpt4 book ai didi

objective-c - 检测 UIWebView 完成以在 iPad 上播放 youtube 视频

转载 作者:太空狗 更新时间:2023-10-30 03:25:04 25 4
gpt4 key购买 nike

我正在使用 UIWebView 在 iPad 上播放 YouTube 视频。

如何检测 YouTube 视频何时播放完毕?我在状态栏中看到播放图标,我尝试使用 MPMusicPlayerController 通知来检测 playbackStateDidChange,但没有成功。

任何想法如何检测此事件?同样,我说的是 iPad,而不是 iPhone。

提前致谢。

更新:

如果您使用零解决方案来检测播放结束,并且还希望 Youtube 视频自动开始,请将 UIWebView 设置为:

self.webView.mediaPlaybackRequiresUserAction = NO ;

我只想澄清一下 YouTube frame API:

"Important: This is an experimental feature, which means that it might change unexpectedly" (08/05/2012)

最佳答案

不,没有办法直接从UIWebView获取网页事件。但是我们可以通过使用 Javascript 来实现这一点。

  • 首先,您在自定义 HTML 中使用嵌入的 Javascript 来检测视频结束播放事件。
  • 然后您尝试使用 JavaScript 加载一个方案定制的请求,UIWebView 可以捕获该请求。

这些链接可能有帮助:

  1. Calling Objective-C from JavaScript in an iPhone UIWebView

  2. Javascript callback in Objective-C

  3. YouTube iFrame API

更新了一个例子:

在 UIWebView 的委托(delegate)中,我输入:

#pragma - mark WebView Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {


if ( [[[request URL] scheme] isEqualToString:@"callback"] ) {

NSLog(@"get callback");

return NO;
}

return YES;

网页在viewDidLoad时加载:

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle       mainBundle] pathForResource:@"youtube" ofType:@"html" ]]]];

在 youtube.html 中我放了:

<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></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: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
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.
var done = false;
function onPlayerStateChange(event) {

if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:anything"; //here's the key
};
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>

你可以看到我添加

if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:anything";
};

到 YouTube 的 iFrame API 演示,它捕获播放器结束播放事件并尝试加载带有方案“回调”的请求,然后 UIWebView Delegate 可以捕获它。

您可以使用此方法通过 JavaScript 触发任何事件;

关于objective-c - 检测 UIWebView 完成以在 iPad 上播放 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9459711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com