2012-01-12 52 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