我正在使用UIWebView在iPad中播放YouTube视频。
如何检测YouTube视频何时播放完毕?我在状态栏中看到了play图标,我尝试使用MPMusicPlayerController通知来检测playbackStateDidChange,但是它没有工作。
有什么办法能发现这个事件吗?再说一次,我说的是iPad而不是iPhone。
提前谢谢。
更新:
如果您使用零解决方案来检测播放结束,并且还希望Youtube视频自动启动,则将UIWebView设置为:
self.webView.mediaPlaybackRequiresUserAction = NO ;我只想澄清一下YouTube框架API:
“重要:这是一个实验性的特性,这意味着它可能会意外地改变”(08/05/2012)
发布于 2012-05-07 02:53:19
不,没有办法直接从UIWebView获得网页事件。但是我们可以通过使用Javascript来实现这一点。
这些链接可能有助于:
用一个例子更新:
在UIWebView的代表中,我指出:
#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代表就可以捕获它。
可以使用此方法使用JavaScript触发任何事件;
发布于 2013-04-24 03:30:24
请参阅:
有一个可用的通知iOS 4.0,您可以使用它来检测youtube视频的完成播放
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubePlayed:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];发布于 2013-02-05 07:44:55
下面是@zero作为一个成熟的类供您使用的奇妙答案:
@interface YouTubeWebView () <UIWebViewDelegate>
@end
@implementation YouTubeWebView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) return nil;
self.mediaPlaybackRequiresUserAction = NO;
self.delegate = self;
self.alpha = 0;
return self;
}
- (void)loadVideo:(NSString *)videoId
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"youtube" ofType:@"html"];
// if (filePath == nil)
NSError *error;
NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
// TODO: error check
string = [NSString stringWithFormat:string, videoId];
NSData *htmlData = [string dataUsingEncoding:NSUTF8StringEncoding];
// if (htmlData == nil)
NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube.html"];
[htmlData writeToFile:targetPath atomically:YES];
[self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:@"callback"]) {
[self removeFromSuperview];
NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube.html"];
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:targetPath error:&error];
// TODO: error check
}
return YES;
}只需使用这个版本的youtube.html,它有一个替换代码(%@)来代替视频id:
<html>
<head><style>body{margin:0px 0px 0px 44px;}</style></head>
<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: '320',
width: '480',
videoId: '%@',
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";
};
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>要实现这一点,我必须克服的唯一主要障碍是将文件作为字符串加载以进行替换。不幸的是,它必须再次编写为一个文件,以便自动播放工作。如果这对于您的用例来说不是必需的,那么可以直接将HTML作为字符串加载到web视图中。
https://stackoverflow.com/questions/9459711
复制相似问题