【问题标题】:MPMoviePlayerController prepareToPlay not working with HTTP Live StreamingMPMoviePlayerController prepareToPlay 不适用于 HTTP 实时流
【发布时间】:2011-09-29 21:29:22
【问题描述】:

我正在尝试在模态视图控制器内使用 MPMoviePlayerController 对象播放 HTTP 实时流视频。如果我像这样运行代码,一切正常。我收到MPMoviePlaybackStatePlaying 通知,视频开始播放。

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayerPlaybackStateDidChange:)  name:MPMoviePlayerPlaybackStateDidChangeNotification  object:nil];

    NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
    self.moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease]; 
    self.moviePlayerController.view.frame = CGRectMake(0,0,320,416);
    self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
    self.moviePlayerController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.moviePlayerController.view];  
    self.moviePlayerController.fullscreen = NO;  
    [self.moviePlayerController play]; 
}

我更喜欢使用prepareToPlay 方法,这样我就可以显示加载指示器,并获得更好的体验。但是,我似乎无法使其正常工作。没有 MPMoviePlayerPlaybackStateDidChangeNotification 被调用,并且流不播放。活动指示器只是坐在那里永远旋转。

我已经确认prepareToPlay 确实被调用了,但在那之后似乎没有其他任何事情发生。我还确认此代码适用于本地电影文件。似乎只是 HTTP Live Streaming 失败了。谁能看到我做错了什么?

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.activityIndicator startAnimating];

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayerPlaybackStateDidChange:)  name:MPMoviePlayerPlaybackStateDidChangeNotification  object:nil];

    NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
    self.moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease]; 

    [self registerForMovieNotifications]; 
}

- (void)registerForMovieNotifications {
    //movie is ready to play notifications
    if ([self.moviePlayerController respondsToSelector:@selector(loadState)]) {
        //this is a 3.2+ device
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];       
        self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
        [self.moviePlayerController prepareToPlay];
    } else {
        //pre-3.2 device
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
    }

    //movie has finished notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification {
    NSLog(@"playbackDidChanged");
    MPMoviePlayerController *moviePlayer = notification.object;
    MPMoviePlaybackState playbackState = moviePlayer.playbackState;
    if(playbackState == MPMoviePlaybackStateStopped) {
        NSLog(@"MPMoviePlaybackStateStopped");
    } else if(playbackState == MPMoviePlaybackStatePlaying) {
        NSLog(@"MPMoviePlaybackStatePlaying");
    } else if(playbackState == MPMoviePlaybackStatePaused) {
        NSLog(@"MPMoviePlaybackStatePaused");
    } else if(playbackState == MPMoviePlaybackStateInterrupted) {
        NSLog(@"MPMoviePlaybackStateInterrupted");
    } else if(playbackState == MPMoviePlaybackStateSeekingForward) {
        NSLog(@"MPMoviePlaybackStateSeekingForward");
    } else if(playbackState == MPMoviePlaybackStateSeekingBackward) {
        NSLog(@"MPMoviePlaybackStateSeekingBackward");
    }
}

//3.2+ devices
- (void)moviePlayerLoadStateChanged:(NSNotification*)notification {
    NSLog(@"load state changed");

    //unless state is unknown, start playback
    if ([self.moviePlayerController loadState] != MPMovieLoadStateUnknown) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

        self.moviePlayerController.view.frame = CGRectMake(0,0,320,416);
        self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
        self.moviePlayerController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:self.moviePlayerController.view];  
        self.moviePlayerController.fullscreen = NO;  
        [self.moviePlayerController play]; 
    }
}

//pre 3.2 devices
- (void) moviePreloadDidFinish:(NSNotification*)notification  {
    // Remove observer
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
    [self.moviePlayerController play];
}

【问题讨论】:

    标签: objective-c mpmovieplayercontroller http-live-streaming


    【解决方案1】:

    我不知道为什么,但我能够通过将行 self.moviePlayerController.controlStyle = MPMovieControlStyleNone; 移出 moviePlayerLoadStateChanged: 方法并进入 viewDidLoad 方法来解决此问题。

    有人知道为什么这会有所作为吗?

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2011-06-05
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多