2012-01-12 75 views
0

我正在将iOS视频嵌入到iOS应用的UIWebView中。我在this YouTube blog post上使用“方法2”来嵌入视频。这很好,除了因为iOS管理媒体播放器,我无法确定视频是播放还是播放完成。我不想在视频播放过程中将视图与另一个视图交换,但我没有看到确定该视图的好方法。有任何想法吗?如果有一种方法可以获得JavaScript回调,那么这将起作用,或者如果有一种方法可以使用HTML5 <video>标签嵌入YouTube视频,那也可以(我尝试过并且没有获得成功)。iOS上带有嵌入式YouTube视频的媒体回调

+1

看到这个问题及其答案:http://stackoverflow.com/questions/8518719/how-to-receive-nsnotifications-from-uiwebview-embedded-youtube-video-playback – Till 2012-01-14 21:53:57

回答

3

你可以注入的JavaScript成UIWebView(见http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview)......其他有趣的东西关于JavaScript和UIWebView可以发现herehere

请尝试将此与此实验性的YouTube API一起使用(请参阅http://code.google.com/apis/youtube/iframe_api_reference.html)......这应该能够让您得到您想要的。

另一个有用的资源,这个从javascript到你的代码回调是here

+0

这就是如果你使用YouTube应用程序来播放视频。我将它们嵌入到“UIWebView”中。在iPhone上,这推动了一个模态视图控制器,但在iPad上,它们以内联方式播放。 – 2012-01-14 17:53:14

+0

我明白 - 因为我没有任何想法......删除我的答案。希望有人可以给你一个工作答案。 – Yahia 2012-01-14 17:57:30

+0

@JeffKelley发现了一个选项...虽然不能测试它... HTH – Yahia 2012-01-14 21:46:24

1

对于iPhone我使用了一些棘手的方法。当视频模式视图控制器被解散时,您可能会收到通知。

-(void) onUIWebViewButtonTouch:(id) sender 
{ 
    self.isWatchForNotifications = YES; 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(windowNowVisible:) 
    name:UIWindowDidBecomeVisibleNotification 
    object:self.view.window 
    ]; 
} 

- (void)windowNowVisible:(NSNotification *)note 
{ 
if (isWatchForNotifications == YES) 
{ 
    //modal viewcontroller was dismissed 
} 
    self.isWatchForNotifications = NO; 
} 
+0

这可行,但只适用于iPhone全屏播放的视频。 – 2012-01-18 15:16:50

9

只需为MPAVControllerPlaybackStateChangedNotification添加观察者即可。

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(playbackStateDidChange:) 
               name:@"MPAVControllerPlaybackStateChangedNotification" 
               object:nil]; 

然后开始听:

- (void)playbackStateDidChange:(NSNotification *)note 
{ 
    NSLog(@"note.name=%@ state=%d", note.name, [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]); 
    int playbackState = [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]; 
    switch (playbackState) { 
     case 1: //end 
      ; 
      break; 
     case 2: //start 
      ; 
      break;  
     default: 
      break; 
    } 
} 

探索其他国家,如果你很好奇。另外大家感兴趣的一群其他通知可以注册即可查看所有:

CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), 
            NULL, 
            noteCallbackFunction, 
            NULL, 
            NULL, 
            CFNotificationSuspensionBehaviorDeliverImmediately); 

然后检查什么是未来的:

void noteCallbackFunction (CFNotificationCenterRef center, 
       void *observer, 
       CFStringRef name, 
       const void *object, 
       CFDictionaryRef userInfo) 
{ 
    NSLog(@"notification name: %@", name); 
    NSLog(@"notification info: %@", userInfo); 
} 

玩得开心!

+0

这种方法可以让我获得95%的体验,但是因为我同时在屏幕上显示多个YouTube播放器,所以不会让我看到哪个视频开始播放。虽然谢谢! – 2012-01-26 01:30:26

+0

您可以从头文件解码更多通知:https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MediaPlayer.framework/MPAVController.h MPAVControllerItemPlaybackDidEndNotification – 2013-03-08 12:53:40