Media Callbacks with Embedded YouTube Videos on iOS

烈酒焚心 提交于 2019-11-29 02:46:51

you can inject javascript into a UIWebView (see http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview)... other interesting stuff about javascript and UIWebView can be found here and here.

try using this together with this experimental youtube API (see http://code.google.com/apis/youtube/iframe_api_reference.html)... this should be able to get you what you want.

Another useful resource for this to make a callback from javascript to your code is here.

Just add observer for MPAVControllerPlaybackStateChangedNotification.

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

then start listening:

- (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;
    }
}

Explore other states if you're curious. Additionally everyone interested in bunch of other notifications can register to see all:

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

then check what's coming on:

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

Have fun!

For iPhone I used some tricky method. You could get a notification when video modal view controller was dismissed.

-(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;
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!