我有兴趣创建一个iPhone应用程序,可以像YouTube一样从中央服务器流式传输视频。 我想知道是否有人以前尝试过这样做,在现有的API等方面有什么最少阻力的路径? 我对这通常是如何完成毫无头绪。 我需要使用套接字吗? 只是在这里寻找一些方向。 谢谢!
我有兴趣创建一个iPhone应用程序,可以像YouTube一样从中央服务器流式传输视频。 我想知道是否有人以前尝试过这样做,在现有的API等方面有什么最少阻力的路径? 我对这通常是如何完成毫无头绪。 我需要使用套接字吗? 只是在这里寻找一些方向。 谢谢!
如果你已经准备好流媒体服务器,那么实现一个弹出式的类似 YouTube 的视频控制器就很容易。
NSString *videoURLString = @"http://path-to-iphone-compliant-video-stream";
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
您需要处理显示视频播放器视图的控制器(在这种情况下是self
)。
在iOS 3.2+中,MPMoviePlayerViewController使其变得更加容易:
NSString *videoURLString = @"http://path-to-iphone-compliant-video-stream";
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerViewController *moviePlayerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:moviePlayerView];
presentMoviePlayerViewControllerAnimated
是MediaPlayer对FWViewController
的补充方法,你可以在iOS 3.2+中找到它,并且它会创建一个视图控制器并将其推送到堆栈上,使用从底部滑动的动画进行动画处理,就像youtube.app一样。
以及最佳实践说明:
https://developer.apple.com/library/content/technotes/tn2224/_index.html
它不仅包含有关流媒体服务架构和构建工具的信息,而且还有一些必须满足的此类服务的要求,并提供了对实时测试流的引用。
使用此代码可使用低内存。 在流视频上....
-(IBAction)playMovie:(NSURL *) theURL
{
NSURL *fileURL = theURL;
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
QuickTime视频已经可以流式传输到手机上了。最简单的方法是使用媒体播放器控制器并将其指向流媒体服务器上的流媒体文件。
虽然现有的答案很好,但如果您需要使用非HTTP流(例如MMS或RTMP)或非苹果支持的音频/视频编解码器,则事情会变得更加复杂。
我本人不是专家,但我一直在使用VideoStreaming SDK来解决这些问题,它使客户端定制变得更加容易(后台流式传输、暂停流等)。如果您也有这些要求,不妨看一看。
2018年的答案 您可以使用AVPlayerViewController
,因为自iOS 9以来MPMoviePlayerController
已被弃用。
NSURL *url = [NSURL URLWithString:videoUrl];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:url];
_playerViewController.player.volume = 1;
_playerViewController.showsPlaybackControls = YES;
_playerViewController.view.frame = CGRectMake(....);
[self.view addSubview:_playerViewController.view];