[英]Play Streaming Video in iPhone
我正在嘗試使用MPMoviewPlayerController通過實時流在我的應用程序中播放視頻,對於那些保存在我的捆綁包中的視頻,它的工作正常,但不適用於實時流,我正在使用它
//for Bundle Video
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"Video.mp4" ofType:nil];
//for Streaming Video
NSURL *videoURL = [NSURL URLWithString:@"http://streaming.disponivel.uol.com.br/video360p2/288148-1192657.mp4"];
self.playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self.view addSubview:self.playerController.view];
self.playerController.view.frame = CGRectMake(0, 0, 320, 400);
[self.playerController.moviePlayer play];
我如何直播視頻?
請使用以下代碼從軟件包中獲取Mp4文件
NSString *url = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
這可以幫助您解決問題嗎
已編輯
NSURL *url = [NSURL URLWithString:@"http://streaming.disponivel.uol.com.br/video360p2/288148-1192657.mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
CGRect frame;
if(self.interfaceOrientation ==UIInterfaceOrientationPortrait)
frame = CGRectMake(20, 69, 280, 170);
else if(self.interfaceOrientation ==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation ==UIInterfaceOrientationLandscapeRight)
frame = CGRectMake(20, 61, 210, 170);
[moviePlayer.view setFrame:frame]; // player's frame must match parent's
[self.view addSubview: moviePlayer.view];
[self.view bringSubviewToFront:moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer prepareToPlay];
[moviePlayer play];
我不知道確切的原因,但MPMoviePlayerController需要是聲明的強屬性。 如果不強烈聲明其屬性,則只會出現黑視圖。
因此,為MPMoviePlayerController聲明一個強大的屬性
@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
和
NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
CGRect frame = CGRectMake(20, 80, 280, 280); //change according to your need
[self.moviePlayer.view setFrame:frame]; // player's frame must match parent's
[self.view addSubview: self.moviePlayer.view];
[self.view bringSubviewToFront:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
和
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSError *error = [[notification userInfo] objectForKey:@"error"];
if (error) {
NSLog(@"Did finish with error: %@", error);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.