在iOS上使用嵌入式YouTube视频进行媒体回调

Jef*_*ley 0 youtube cocoa-touch uiwebview ios

我正在将UIWebViewiOS 视频嵌入到iOS应用中.我在YouTube博客文章中使用"方法2" 来嵌入视频.这很有效,除了因为iOS管理媒体播放器,我无法确定视频是播放还是已完成.在播放视频时,我不想将该视图与另一视图交换,但我认为没有一种好方法可以确定.有任何想法吗?如果有办法获得JavaScript回调,那将会有效,或者如果有办法使用HTML5 <video>标签嵌入YouTube视频,那么这也会有效(我已经尝试过但没有取得成功).

Les*_*Nie 9

只需为MPAVControllerPlaybackStateChangedNotification添加观察者.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateDidChange:)
                                                 name:@"MPAVControllerPlaybackStateChangedNotification"
                                               object:nil];
Run Code Online (Sandbox Code Playgroud)

然后开始听:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你好奇,探索其他州.此外,每个对其他通知感兴趣的人都可以注册查看全部:

CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), 
                                    NULL, 
                                    noteCallbackFunction, 
                                    NULL, 
                                    NULL,  
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
Run Code Online (Sandbox Code Playgroud)

然后检查发生了什么:

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

玩得开心!