如何在iOS中使用MPMoviePlayerController播放视频流

der*_*lke 16 iphone stream mpmovieplayercontroller media-player ios

我试图通过按下按钮在iPhone上播放来自互联网的视频流.我使用了很多代码示例但没有任何效果.使用此代码,它会打开一个黑色视图,其中没有任何视频流或控件.(流本身有效.)

NSURL *url = [NSURL URLWithString:@"http://MyStreamURL.com"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
Run Code Online (Sandbox Code Playgroud)

jon*_*oll 27

Instead of creating a MPMoviePlayerController and adding that to your view, it is probably simpler to create a MPMoviePlayerViewController and present that view controller modally (since you are trying to show your video full screen anyway). Then the MPMoviePlayerViewController can manage the presentation of your video for you.

MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(moviePlaybackDidFinish:)
                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                       object:nil];    

mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

[self presentMoviePlayerViewControllerAnimated:mpvc];
[mpvc release];
Run Code Online (Sandbox Code Playgroud)

在您的moviePlayBackDidFinish委托方法中,您可以关闭模​​型视图控制器.

  • 但是为什么上面的方法与MPMoviePlayerController不起作用呢? (5认同)
  • 但是小的修正; 你应该使用`presentMoviePlayerViewControllerAnimated`而不是`presentModalViewController`.这将导致用户期望从电影播放器​​获得适当的动画效果. (2认同)