I'm making this iPhone app that allows people to watch youtube videos and vimeo videos. I was wondering if there's a custom player I can incorporate into my app that will play the video from the youtube link or vimeo link. This has to be iOS 7 compatible as well. Thanks!
-
whats the problem with a webview ?? – Kunal Balani Nov 21 '13 at 02:27
-
The webview has the embedded youtube video player (which looks un professional in my opinion). There's an app called ProTuber that does this same exact thing. – Username Nov 21 '13 at 02:52
-
wrong. ProTuber uses the webview with adding custom toolbars on top. check this http://stackoverflow.com/questions/9060153/play-youtube-videos-in-iphone-app-without-using-uiwebview – Kunal Balani Nov 21 '13 at 02:54
-
@KunalBalani Does this also allow for custom controls? and custom appearance of player as well? – Username Nov 21 '13 at 03:05
-
@KunalBalani, on ProTuber the video plays without automatically going to full screen, how is that possible with the UIWebView method? – Username Nov 21 '13 at 03:19
-
Please do some research on your own. This is a very common problem and has been solved several times. http://stackoverflow.com/questions/15717754/objective-c-how-to-autoplay-a-youtube-video-in-a-uiwebview – Kunal Balani Nov 21 '13 at 04:11
-
Also, I have added link for YOutubehacks which shows , how to get play , pause feature. check my answer below – Kunal Balani Nov 21 '13 at 04:13
2 Answers
Possibly you are looking for AVFoundation.framework. Just add it to your project and you can use its AVPlayer component to allow a total customization of your interface. It would be something like this:
-(void)viewDidLoad {
//prepare player
self.videoPlayer = [AVPlayer playerWithURL:<# NSURL of your youtube video #>];
self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
//prepare player layer
self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
self.videoPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.videoPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.videoPlayerLayer];
//add player item status notofication handler
[self addObserver:self forKeyPath:@"videoPlayer.currentItem.status" options:NSKeyValueObservingOptionNew context:NULL];
//notification handler when player item completes playback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}
//called when playback completes
-(void)playerItemDidReachEnd:(NSNotification *)notification {
[self.videoPlayer seekToTime:kCMTimeZero]; //rewind at the end of play
//other tasks
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"videoPlayer.currentItem.status"]) {
//NSLog(@"player status changed");
if (self.videoPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay) {
//player is ready to play and you can enable your playback buttons here
}
}
}
And like normal VC you can add buttons or other items to invoke these methods for playback:
-(IBAction)play:(id)sender {
[self.videoPlayer play];
}
-(IBAction)pause:(id)sender {
[self.videoPlayer pause];
}
Make sure that you remove the observers previously added before you leave the VC or else those are gonna leak:
//remove observers
@try {
[self removeObserver:self forKeyPath:@"videoPlayer.currentItem.status" context:NULL];
}
@catch (NSException *exception) {}
@try {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}
@catch (NSException *exception) {}
A detailed explanation from Apple is available here. Hope you would find it helpful.

- 5,238
- 2
- 28
- 40
-
Would all this go in the .h (after importing the AVFoundation framework)? – Username Nov 21 '13 at 02:53
-
-
Have you actually tried this? Does it really work to play **YouTube** videos passing the video URL? – cprcrack May 02 '14 at 10:05
1) Youtube uses flash which does not work with native mediaplayer. 2) Native media player either needs a file or it can live stream url which use HTTPLiveStreaming (it does not support RTSP).
In simple words they work on different protocol and can't talk to each other. However, you can use third party API's to download youtube video and then play but this requires your user to wait until the entire file is downloaded.
I would recommend you to use webview. here is a good article which shows how to do so. YoutubeHacks is an opensource tool for the same.

- 4,739
- 4
- 36
- 73
-
Youtube I think has an HLS version, another way would be to use a 3rd party rtsp player and the android mobile rtsp link. ie. the link youtube provides for android players. http://streammore-tv.tumblr.com/post/34794206547/welcome this beta program is expired but there are other players. – Michelle Cannon Nov 21 '13 at 21:18