如何在 iOS 中使用 MPMoviePlayerController 播放视频流 [英] How to play video stream with MPMoviePlayerController in iOS

查看:38
本文介绍了如何在 iOS 中使用 MPMoviePlayerController 播放视频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过按下按钮在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆