繁体   English   中英

在iPhone中播放流式视频

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM