iOS播放视频黑屏?

标签 ios

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

更新

- (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。我不知道为什么。

最佳答案

为 MPMoviePlayerController 创建属性,因为您在将其添加为 subview 后保留 View ,但不保留 Controller 。

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

关于iOS播放视频黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062103/

相关文章:

ios - 通过SDK登录到Facebook后,它仅停留在浏览器窗口中

android - Firebase 性能监控 React Native 集成

javascript - slider 相对于 slider 拇指的显示值 native react

ios - 通过单击左右按钮操作将 UICollectionView 的单元格滚动到左右方向

ios - 如何通过 Google Ads 跟踪 iOS 安装?

ios - 带部分的 UiTableView 的数据 + UISegmentedControl

ios - UICollectionView、分页和 clipsToBounds

ios - _CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer 的 undefined symbol

ios - 基于二进制数据设置 UIImageView 的图像

ios - 批准失败