如何在 iOS 中使用 MPMoviePlayerController 播放视频流
我正在尝试通过按按钮在 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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建
MPMoviePlayerViewController
并以模态方式呈现该视图控制器可能更简单,而不是创建MPMoviePlayerController
并将其添加到您的视图中(因为您试图显示完整的视频)无论如何屏幕)。然后 MPMoviePlayerViewController 可以为您管理视频的呈现。在您的moviePlayBackDidFinish 委托方法中,您可以关闭模型视图控制器。
Instead of creating a
MPMoviePlayerController
and adding that to your view, it is probably simpler to create aMPMoviePlayerViewController
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.In your
moviePlayBackDidFinish
delegate method you can then dismiss the model view controller.需要提及电影源类型为流媒体
Need to mention movie source type as streaming
在“链接库”部分中添加 AVFoundation 框架
在您的 .h 文件中添加
在您的 .m 文件中
Add AVFoundation frame work in Link Libraries section
In your .h file add
In your .m file
只需将“
MPMovieSourceTypeStreaming
”添加到“moviesourcetype
”just add "
MPMovieSourceTypeStreaming
" to "moviesourcetype
"