I’m embedding YouTube videos in a UIWebView
for an iOS app. I’m using “Method 2” at this YouTube blog post to embed the video. This works great, except that because iOS manages the media player, I can’t determine whether the video is playing or is done. I don’t want to swap that view out with another one while the video is playing, but I don’t see a good way to determine that. Any ideas? If there’s a way to get a JavaScript callback, that will work, or if there’s a way to embed YouTube videos using the HTML5 <video>
tag, that will work as well (I’ve tried that and not gotten success).

- 19,021
- 6
- 70
- 80
-
1See this question and its answers: http://stackoverflow.com/questions/8518719/how-to-receive-nsnotifications-from-uiwebview-embedded-youtube-video-playback – Till Jan 14 '12 at 21:53
3 Answers
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!

- 665
- 13
- 17
-
This approach would get me 95% of the way there, but since I have multiple YouTube players on screen at once, it wouldn’t allow me to see *which* video started. Thanks though! – Jeff Kelley Jan 26 '12 at 01:30
-
You could decode the more Notifications from the header file: https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MediaPlayer.framework/MPAVController.h e.g. MPAVControllerItemPlaybackDidEndNotification – Morten Holmgaard Mar 08 '13 at 12:53
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.

- 69,653
- 9
- 115
- 144
-
That’s if you use the YouTube app to play videos. I’m embedding them in a `UIWebView`. On iPhone, this pushes a modal view controller, but on iPad, they play inline. – Jeff Kelley Jan 14 '12 at 17:53
-
I understands - for that I don't have any idea... deleting my answer. hope someone can give your a working answer. – Yahia Jan 14 '12 at 17:57
-
-
Unfortunately this is for a high-volume application. From the API link: “Note: The IFrame player API described in this document is an experimental feature that represents the next stage of YouTube Player APIs. You should not yet build business-critical applications using this API nor should you launch any applications using this API into a production environment.” – Jeff Kelley Jan 14 '12 at 21:56
-
@JeffKelley that's why I wrote *experimental*... although I would expect Google to make this API official at some point... don't know your timeframe - perhaps it is worth a shot ? – Yahia Jan 14 '12 at 21:58
-
-
This works well enough for my immediate needs. I’m still a little concerned about the experimental nature of the API, but it looks like it’s been a while since people started using it on iOS, so I’ll have to be content with it. Thanks! – Jeff Kelley Jan 15 '12 at 05:01
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;
}

- 12,359
- 18
- 80
- 130