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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8864405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:18:55  来源:igfitidea点击:

How to play video stream with MPMoviePlayerController in iOS

iosiphonestreammedia-playermpmovieplayercontroller

提问by derFalke

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.)

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

回答by jonkroll

Instead of creating a MPMoviePlayerControllerand adding that to your view, it is probably simpler to create a MPMoviePlayerViewControllerand 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.

与其创建 aMPMoviePlayerController并将其添加到您的视图中,不如创建 aMPMoviePlayerViewController并以模态方式呈现该视图控制器可能更简单(因为无论如何您都试图全屏显示视频)。然后 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];

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

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

回答by Rajesh Loganathan

Need to mention movie source type as streaming

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

moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

回答by Rahul K Rajan

Add AVFoundation frame work in Link Libraries section

在链接库部分添加 AVFoundation 框架工作

In your .h file add

在您的 .h 文件中添加

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

In your .m file

在您的 .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];

回答by Sabby

just add "MPMovieSourceTypeStreaming" to "moviesourcetype"

只需将“ MPMovieSourceTypeStreaming”添加到“ moviesourcetype