我没有收到有关的任何通知MPMoviePlayerController
。我究竟做错了什么?
我使用以下逻辑。
我开始在中播放youtube视频UIWebView
。UIWebView
称为标准MPMoviePlayerController
。我没有控制,MPMoviePlayerController
因为我没有实例化MPMoviePlayerController
。
我以自动播放的方式运行youtube的剪辑(延迟1秒):
[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];
我的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[self embedYouTube];
}
- (void)loadStateDidChange:(NSNotification*)notification
{
NSLog(@"________loadStateDidChange");
}
- (void)playbackDidFinish:(NSNotification*)notification
{
NSLog(@"________DidExitFullscreenNotification");
}
- (void)embedYouTube
{
CGRect frame = CGRectMake(25, 89, 161, 121);
NSString *urlString = [NSString stringWithString:@"http://www.youtube.com/watch?v=sh29Pm1Rrc0"];
NSString *embedHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
videoView.delegate = self;
for (id subview in videoView.subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView
{
UIButton *b = [self findButtonInView:_webView];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(touchInView:) object:b];
[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];
}
- (UIButton *)findButtonInView:(UIView *)view
{
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0)
{
for (UIView *subview in view.subviews)
{
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
- (void)touchInView:(UIButton*)b
{
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
更新:我正在创建播放youtube视频的应用程序。您可以运行播放列表,您将看到第一个视频。当第一个视频结束时,第二个视频开始自动播放,依此类推。
我需要支持ios 4.1及更高版本。
UPDATE2: @ H2CO3我正在尝试使用您的url方案,但是它不起作用。退出事件未调用委托方法。我将我的html网址添加到日志中。它是:
<html><head> <body style="margin:0">
<script>function endMovie()
{document.location.href="somefakeurlscheme://video-ended";}
</script> <embed id="yt" src="http://www.youtube.com/watch?v=sh29Pm1Rrc0"
onended="endMovie()" type="application/x-shockwave-flash"
width="161" height="121"></embed>
</body></html>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:@"somefakeurlscheme://video-ended"])
{
[self someMethodSupposedToDetectVideoEndedEvent];
return NO; // prevent really loading the URL
}
return YES; // else load the URL as desired
}
UPDATE3 @直到,我无法捕获UIMoviePlayerControllerDidExitFullscreenNotification,但是找到了MPAVControllerItemPlaybackDidEndNotification。播放视频结束时,将显示MPAVControllerItemPlaybackDidEndNotification。
但是我不明白如何捕获onDone通知?
UIWebView
嵌入式电影播放器没有发送记录的通知。
实际上,文档中使用的封闭式实现在许多方面(例如DRM)UIWebView
与公众有所不同MPMoviePlayerController
。
用于播放视频内容的最重要的类UIWebView
称为MPAVController
和UIMoviePlayerController
。后者使播放器看起来像MPMoviePlayerController
全屏界面。
如果您敢冒险遭到苹果公司的拒绝,则实际上还有一些方法可以实现您所寻找的目标。
注意此文档没有记录,并且在每个新的iOS版本中都会中断。但是,它确实适用于iOS4.3、5.0和5.01、5.1和6.0,并且可能也适用于其他版本。
我无法在iOS 4.1和4.2上测试此解决方案,因此您可以自行决定。我高度怀疑它会起作用。
全屏状态
例如,如果您打算对用户点击“完成”按钮做出反应,则可以通过以下方式进行操作:
更新 建议使用此答案的旧版本, UIMoviePlayerControllerDidExitFullscreenNotification
而此新版本(针对iOS6更新)建议使用 UIMoviePlayerControllerWillExitFullscreenNotification
。
C语言等级:
void PlayerWillExitFullscreen (CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
//do something...
}
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
NULL,
PlayerWillExitFullscreen,
CFSTR("UIMoviePlayerControllerWillExitFullscreenNotification"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
Objective-C级:
- (void)playerWillExitFullscreen:(NSNotification *)notification
{
//do something...
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerWillExitFullscreen:)
name:@"UIMoviePlayerControllerWillExitFullscreenNotification"
object:nil];
我确实草拟了C级和Objective-C级选项,因为真正了解所有这些的最佳方法是使用C级(CoreFoundation)函数,如我的答案结尾所示。如果一个通知的发件人不使用的Objective-C(NSNotifications),你可能实际上没有使用NSNotification力学能够捕获它们。
播放状态
要检查播放状态,请注意"MPAVControllerPlaybackStateChangedNotification"
(如上图所示),然后检查userInfo
可能看起来像这样的内容:
{
MPAVControllerNewStateParameter = 1;
MPAVControllerOldStateParameter = 2;
}
进一步的逆向工程
要进行逆向工程并浏览所有发送的通知,请使用以下代码段。
void MyCallBack (CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
NSLog(@"name: %@", name);
NSLog(@"userinfo: %@", userInfo);
}
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
NULL,
MyCallBack,
NULL,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句