如何在iOS中使用MPMoviePlayerController播放视频流 [英] How to play video stream with MPMoviePlayerController in iOS
问题描述
我正试图通过按下按钮在iPhone上播放来自互联网的视频流。
我使用了很多代码示例,但没有任何效果。使用此代码,它会打开一个黑色视图,其中没有任何视频流或控件。 (流本身有效。)
I am trying to play a video stream from the internet on the iPhone by pressing a button. I used many code samples but nothing worked. With this code it opens a black view without any video stream or controls in it. (The stream itself works.)
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];
推荐答案
而不是创建 MPMoviePlayerController
并将其添加到您的视图中,创建 MPMoviePlayerViewController
并以模态方式显示该视图控制器(因为您尝试显示视频)可能更简单无论如何全屏)。然后MPMoviePlayerViewController可以为您管理视频的演示。
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];
在 moviePlayBackDidFinish
委托方法中,您可以关闭模型视图控制器。
In your moviePlayBackDidFinish
delegate method you can then dismiss the model view controller.
这篇关于如何在iOS中使用MPMoviePlayerController播放视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!