我正在制作这个iPhone应用程序,允许人们观看YouTube视频和vimeo视频。我想知道是否有一个自定义播放器我可以合并到我的应用程序中,它将播放来自youtube链接或vimeo链接的视频。这也必须兼容iOS 7。谢谢!
答案 0 :(得分:0)
可能你正在寻找AVFoundation.framework。只需将其添加到您的项目中,您就可以使用其AVPlayer组件来完全自定义您的界面。它会是这样的:
-(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
}
}
}
与普通VC一样,您可以添加按钮或其他项目来调用这些方法进行播放:
-(IBAction)play:(id)sender {
[self.videoPlayer play];
}
-(IBAction)pause:(id)sender {
[self.videoPlayer pause];
}
确保在离开VC之前删除之前添加的观察者,否则这些观察者会泄漏:
//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) {}
Apple提供了详细解释here。希望你会发现它有用。
答案 1 :(得分:0)
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).
简单来说,他们使用不同的协议,不能互相交谈。 但是,您可以使用第三方API下载YouTube视频然后播放,但这需要您的用户等到整个文件下载完毕。
我建议你使用webview。 here是一篇很好的文章,展示了如何做到这一点。 YoutubeHacks是一个开源工具。