2010-06-04 140 views
23

我有兴趣创建可从中央服务器以YouTube风格传输视频的iPhone应用程序。我想知道是否有人曾尝试过这样做,什么是阻力最小,现有API等的路径?我完全不知道这通常是如何完成的。我会使用套接字吗?只是在这里寻找一些方向。谢谢!编写应用程序将视频流式传输到iPhone

回答

19

如果您已经准备好流媒体服务器,那么实现弹出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中,使用从底部滑动的动画制作动画。

+0

这不是为我工作的任何方式已经过时了。 – Deepukjayan 2012-08-23 11:10:03

+0

这个代码用于流式传输吗? – Dilip 2013-03-01 13:38:48

2

QuickTime视频已经传输到手机。阻力最小的路径是使用媒体播放器控制器,并将其指向流媒体服务器上的流媒体文件。

7

苹果公司有关设置服务器端的流媒体一个详细的文章:

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/Introduction/Introduction.html

和最佳做法注:

https://developer.apple.com/library/content/technotes/tn2224/_index.html

它不仅包含流媒体服务的架构信息以及用于构建它的工具,但对于必须实现的此类服务以及参考实时测试流也有一些要求。

3

使用此代码使用低内存。在视频流....

-(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]; 
} 
1

虽然现有的回答都不错,如果你需要使用非HTTP流(MMS或RTMP举例来说)还是非苹果支持的音频/视频编解码器,事情就变得有点复杂。

我自己并不是专家,但我一直在使用VideoStreaming SDK来解决这些问题,它使定制客户端变得更容易(后台流,暂停流等)。如果你有这些要求,也许值得一看。

0

2018回答您可以使用AVPlayerViewController因为MPMoviePlayerController以来的iOS 9

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]; 
相关问题