iOS播放视频黑屏?

时间:2012-06-16 09:01:25

标签: ios

我一直在浏览Google以获得各种解释,但我仍然无法弄清楚此代码何时触发屏幕是黑色的。有人能发现错误吗?

更新

- (IBAction)playVideo:(id)sender {
    NSURL *videoUrl = [[DataStore singletonInstance] getVideoUrl:self withUuid:self.eventDetailVC.event.uuid];
    if ([videoUrl checkResourceIsReachableAndReturnError:nil] == NO) {
        NSLog(@"Video doesn't not exist.");
        return;
    }
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:player];
    [previewView addSubview:player.view];
    player.view.frame = previewView.bounds;
    player.controlStyle = MPMovieControlStyleDefault;
    [player play];
}

- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    NSLog(@"moviePlayBackDidFinish: called");
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:player];
    // Checking for errors
    NSDictionary *notiUserInfo = [notification userInfo];
    if (notiUserInfo != nil) {
        NSError *errorInfo = [notiUserInfo objectForKey:@"error"];
        if ([[errorInfo domain] isEqualToString:@"MediaPlayerErrorDomain"]) {
            UIAlertView *notice = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                             message:[errorInfo localizedDescription] 
                                                            delegate:self 
                                                   cancelButtonTitle:@"OK" 
                                                   otherButtonTitles:nil];
            [notice show];
            return;
        }
    }
    // Remove from view
    [player.view removeFromSuperview];
    [player stop];
}

根本不会调用FYI moviePlayBackDidFinish。我不知道为什么。

1 个答案:

答案 0 :(得分:9)

为MPMoviePlayerController创建属性,因为在将其添加为子视图后保留视图,但不保留控制器。

@property (strong, nonatomic) MPMoviePlayerController *player;

...

@synthesize player = _player;

...

- (IBAction)playVideo:(id)sender
{
    NSURL *videoUrl = [[DataStore singletonInstance] getVideoUrl:self withUuid:self.eventDetailVC.event.uuid];
    if ([videoUrl checkResourceIsReachableAndReturnError:nil] == NO)
    {
        NSLog(@"Video doesn't not exist.");
        return;
    }
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil];
    [previewView addSubview:_player.view];
    _player.view.frame = previewView.bounds;
    _player.controlStyle = MPMovieControlStyleDefault;
    [_player play];
}

- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
    NSLog(@"moviePlayBackDidFinish: called");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:nil];
    // Checking for errors
    NSDictionary *notiUserInfo = [notification userInfo];
    if (notiUserInfo != nil)
    {
        NSError *errorInfo = [notiUserInfo objectForKey:@"error"];
        if ([[errorInfo domain] isEqualToString:@"MediaPlayerErrorDomain"])
        {
            UIAlertView *notice = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                             message:[errorInfo localizedDescription] 
                                                            delegate:self 
                                                   cancelButtonTitle:@"OK" 
                                                   otherButtonTitles:nil];
            [notice show];
            return;
        }
    }
    // Remove from view
    [_player.view removeFromSuperview];
    [_player stop];
    self.player = nil;
}