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

发布于 2024-12-27 04:16:20 字数 759 浏览 0 评论 0原文

我正在尝试通过按按钮在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我为君王 2025-01-03 04:16:20

创建 MPMoviePlayerViewController 并以模态方式呈现该视图控制器可能更简单,而不是创建 MPMoviePlayerController 并将其添加到您的视图中(因为您试图显示完整的视频)无论如何屏幕​​)。然后 MPMoviePlayerViewController 可以为您管理视频的呈现。

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 委托方法中,您可以关闭模​​型视图控制器。

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];

In your moviePlayBackDidFinish delegate method you can then dismiss the model view controller.

满地尘埃落定 2025-01-03 04:16:20

需要提及电影源类型为流媒体

moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

Need to mention movie source type as streaming

moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
三寸金莲 2025-01-03 04:16:20

在“链接库”部分中添加 AVFoundation 框架

在您的 .h 文件中添加

#import <MediaPlayer/MediaPlayer.h>
@interface video_liveViewController : UIViewController<MPMediaPickerControllerDelegate,MPMediaPlayback>

在您的 .m 文件中

NSURL *movieURL = [NSURL URLWithString:@"http://172.31.17.252:1935/live/myStream/playlist.m3u8"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];

Add AVFoundation frame work in Link Libraries section

In your .h file add

#import <MediaPlayer/MediaPlayer.h>
@interface video_liveViewController : UIViewController<MPMediaPickerControllerDelegate,MPMediaPlayback>

In your .m file

NSURL *movieURL = [NSURL URLWithString:@"http://172.31.17.252:1935/live/myStream/playlist.m3u8"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];
酷到爆炸 2025-01-03 04:16:20

只需将“MPMovieSourceTypeStreaming”添加到“moviesourcetype

just add "MPMovieSourceTypeStreaming" to "moviesourcetype"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文