我正试图通过按下按钮在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];
与其创建MPMoviePlayerController
并将其添加到视图中,不如创建MPMoviePlayerViewController
并以模式显示视图控制器(因为您无论如何都要尝试全屏显示视频)。然后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
委托方法中,可以取消模型视图控制器。
需要提到电影源类型作为流
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
"