【问题标题】:iPhone SDK Custom Video Player with YouTube [duplicate]带有 YouTube 的 iPhone SDK 自定义视频播放器 [重复]
【发布时间】:2013-12-05 08:17:52
【问题描述】:

我正在制作这款 iPhone 应用程序,它允许人们观看 youtube 视频和 vimeo 视频。我想知道是否可以将自定义播放器合并到我的应用程序中,以播放来自 youtube 链接或 vimeo 链接的视频。这也必须与 iOS 7 兼容。谢谢!

【问题讨论】:

  • webview 有什么问题??
  • webview 有嵌入式 youtube 视频播放器(在我看来这看起来不专业)。有一个名为 ProTuber 的应用可以做同样的事情。
  • 错误。 ProTuber 使用 web 视图并在顶部添加自定义工具栏。检查这个stackoverflow.com/questions/9060153/…
  • @KunalBalani 这是否也允许自定义控件?以及播放器的自定义外观?
  • @KunalBalani,在 ProTuber 上播放视频时不会自动全屏播放,使用 UIWebView 方法怎么可能?

标签: ios iphone ios7 video-streaming


【解决方案1】:

您可能正在寻找 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 获得。希望对您有所帮助。

【讨论】:

  • 所有这些都会放在 .h 中吗(在导入 AVFoundation 框架之后)?
  • 一点也不...你怎么期望viewDidLoad出现在.h中!!!
  • 你真的试过这个吗?通过视频 URL 播放 YouTube 视频真的有效吗?
【解决方案2】:
1) Youtube 使用的 flash 不适用于本机媒体播放器。 2)原生媒体播放器要么需要一个文件,要么它可以直播使用 HTTPLiveStreaming 的流 url(它不支持 RTSP)。

简而言之,它们使用不同的协议,不能相互交谈。 但是,您可以使用第三方 API 下载 youtube 视频然后播放,但这需要您的用户等到整个文件下载完毕。

我建议你使用 webview。 here 是一篇很好的文章,它展示了如何做到这一点。 YoutubeHacks 是一个开源工具。

【讨论】:

  • Youtube 我认为有 HLS 版本,另一种方法是使用 3rd 方 rtsp 播放器和 android 移动 rtsp 链接。 IE。 youtube 为 android 播放器提供的链接。 streammore-tv.tumblr.com/post/34794206547/welcome此测试程序已过期,但还有其他玩家。
猜你喜欢
  • 2011-09-18
  • 2010-12-19
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多