我正试图通过按下按钮在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];
答案 0 :(得分:27)
而不是创建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
委托方法中,您可以关闭模型视图控制器。
答案 1 :(得分:2)
需要提及电影源类型为流媒体
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
答案 2 :(得分:1)
在链接库部分
中添加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];
答案 3 :(得分:-1)
只需添加&#34; MPMovieSourceTypeStreaming
&#34;到&#34; moviesourcetype
&#34;