【问题标题】:iOS Play video black screen?iOS播放视频黑屏?
【发布时间】:2012-06-16 09:01:24
【问题描述】:

我一直在通过谷歌浏览各种解释,但我仍然无法弄清楚这段代码何时触发屏幕是漆黑的。任何人都能够发现错误?

更新

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

仅供参考,moviePlayBackDidFinish 根本没有被调用。我不知道为什么。

【问题讨论】:

  • 你是在模拟器还是设备中检查这个?
  • 能否附上视频网址?它可能与内容本身有关。
  • 就像@stavash 建议的那样,这可能是一个内容问题。我已经看到当视频内容不是视频播放器支持的格式时会发生这种情况。我会先尝试其他视频,看看你是否能成功播放它们,这样你至少可以确定这是否只是内容问题。
  • 仅供参考,视频是在 iphone 上拍摄的,所以我不知道这是否真的是问题所在。
  • 尝试注册MPMoviePlayerLoadStateDidChangeNotification 并在选择器中记录loadState 以缩小可能性。

标签: ios


【解决方案1】:

为 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;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 2014-04-03
相关资源
最近更新 更多