【问题标题】:Play Streaming Video in iPhone在 iPhone 中播放流媒体视频
【发布时间】:2026-02-22 10:45:01
【问题描述】:

我正在尝试使用 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];

我如何流式传输实时视频?

【问题讨论】:

  • 您发布的 URL 指向位于网络服务器上的文件,而不是流。 MPMoviePlayerViewController 将接受流,但不仅仅是位于服务器上的文件。
  • 能否请您提供一些流媒体链接,我尝试了几次但无法正常工作
  • 我想使用 MPMoviePlayer 播放视频,而不是在 webview 中

标签: iphone ios objective-c xcode mpmoviewcontroller


【解决方案1】:

请使用以下代码从包中获取 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];

【讨论】:

  • 如果视频是捆绑的,但我想从像我自己这样的服务器流式传输
  • 黑色视图不工作,几秒钟后调用moviePlayBackDidFinish
  • @MuzamilHassan 我不知道确切的原因,但 MPMoviePlayerController 需要声明一个强属性。如果您不强烈声明其属性,则只会出现黑色视图。请查看我的更新答案。
【解决方案2】:

我不知道确切的原因,但 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);
     }
 }

【讨论】: