如何在iOS中使用MPMoviePlayerController播放videostream

我试图通过按下button从iPhone上的互联网播放videostream。 我用了很多代码示例,但没有任何工作。 有了这个代码,它将打开一个没有任何videostream或控件的黑色视图。 (stream本身起作用)

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

而不是创build一个MPMoviePlayerController并将其添加到您的视图中,创build一个MPMoviePlayerViewController并以模态方式呈现该视图控制器(因为您试图无论如何显示您的video全屏)可能更简单。 然后,MPMoviePlayerViewController可以pipe理您的video的演示文稿。

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

需要提及电影来源types为stream媒体

 moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; 

在链接库部分添加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]; 

只需将“ MPMovieSourceTypeStreaming ”添加到“ moviesourcetype