2

我试图在模式视图控制器内使用MPMoviePlayerController对象播放HTTP实时流式视频。如果我像这样运行代码,一切正常。我收到一条MPMoviePlaybackStatePlaying通知,并播放视频。MPMoviePlayerController prepareToPlay不支持HTTP实时流式传输

- (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实时流传输失败。任何人都可以看到我做错了什么?

- (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]; 
} 

回答

5

我不知道为什么,但我可以通过移动线self.moviePlayerController.controlStyle = MPMovieControlStyleNone;moviePlayerLoadStateChanged:的方法来解决这个问题,而进入viewDidLoad方法。

任何人有任何想法,为什么这有所作为?

相关问题