2

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

1 回答 1

5

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

任何人都知道为什么这会有所作为?

于 2011-09-29T22:08:39.857 回答