ios - 在 iOS 6 上开始播放视频时 MPMoviePlayerController 崩溃

标签 ios video mpmovieplayercontroller mpmoviewcontroller

我正在使用 MPMoviePlayerViewController 播放视频。在我的应用程序中,我使用标准的 MPMoviePlayerController 类播放应用程序中的视频。它在 iOS 7 和 8 上运行良好

第一次解决这个问题时效果很好,但是在观看了 1 个视频后,如果您尝试观看其他内容,应用程序会在 MPMoviePlayerController 的播放方法上崩溃并出现错误:

: CGContextSaveGState: invalid context 0x0

: CGContextClipToRect: invalid context 0x0

: CGContextTranslateCTM: invalid context 0x0

: CGContextDrawShading: invalid context 0x0

: CGContextRestoreGState: invalid context 0x0

*** -[MPMoviePlayerController retain]: message sent to deallocated instance 0x1e5718b0

我不明白为什么会发生这种情况。

这是我的代码:

AFPlayerViewController.h

@property (strong, nonatomic) MPMoviePlayerViewController *playerViewController;

- (id)initWithContentURL:(NSString*)movieUrl
         andSubtitlePath:(NSString*)subPath
          withTypeServer:(NSString*)serverType
                withName:(NSString*)name
           withEpisodeId:(NSString*)episodeId
              withFilmId:(NSString*)filmId
            withDuration:(NSInteger)targetDuration
              withSeason:(NSString*)seasonId;

AFPlayerViewController.m

@synthesize playerViewController;

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (_movieURL) {
        self.playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL: _movieURL];
        self.playerViewController.moviePlayer.initialPlaybackTime = -1.f;
        self.playerViewController.moviePlayer.shouldAutoplay = NO;
        [self.playerViewController.moviePlayer setControlStyle:MPMovieControlStyleNone];
        [self.playerViewController.moviePlayer setFullscreen:NO animated:YES];
        self.playerViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

        [self.playerViewController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

        [self.view addSubview:self.playerViewController.view];
        [NSLayoutConstraint alightTopBotLeftRight:self.playerViewController.view inView:self.view];

        [self.playerViewController.moviePlayer prepareToPlay];
        [self.playerViewController.moviePlayer play];

        [self receiveNotificationPlayerViewController];
    }
}

- (void)receiveNotificationPlayerViewController {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(durationAvailable:)
                                                 name:MPMovieDurationAvailableNotification
                                               object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loadStateDidChange:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateDidChange:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(sourceTypeAvailable:)
                                                 name:MPMovieSourceTypeAvailableNotification
                                               object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(readyForDisplay:)
                                                 name:MPMoviePlayerReadyForDisplayDidChangeNotification
                                               object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didFinishAVideo:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.playerViewController.moviePlayer];
}

- (void) durationAvailable: (NSNotification*) notification {
    NSLog(@"1");
}

- (void) loadStateDidChange: (NSNotification*) notification {
    NSLog(@"2");
    if ([self.playerViewController.moviePlayer loadState] != MPMovieLoadStateUnknown) {
        [[NSNotificationCenter  defaultCenter] removeObserver:self
                                                         name:MPMoviePlayerLoadStateDidChangeNotification
                                                       object:nil];
    }
}

- (void) playbackStateDidChange: (NSNotification*) notification {
    NSLog(@"3");
    switch (self.playerViewController.moviePlayer.playbackState) {
        case MPMoviePlaybackStateSeekingForward:
        case MPMoviePlaybackStateSeekingBackward:
            break;
        case MPMoviePlaybackStatePaused: {
            NSLog(@"pause");
        }
            break;
        case MPMoviePlaybackStateStopped: {
            NSLog(@"stop");
        }
            break;
        case MPMoviePlaybackStateInterrupted:{
            NSLog(@"interrupted");
        }
            break;
        case MPMoviePlaybackStatePlaying: {
            NSLog(@"playing");
        }
            break;
        default:
            break;
    }
}

- (void) sourceTypeAvailable: (NSNotification*) notification {
    NSLog(@"4");
}

- (void) readyForDisplay: (NSNotification*) notification {
    NSLog(@"5");
}

- (void)orientationDidChange: (NSNotification*)notification {
    NSLog(@"6");
}

- (void)didFinishAVideo:(NSNotification*)notification {
    [self removeNotification];
}

- (void)removeNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMovieDurationAvailableNotification
                                                  object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                                  object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackStateDidChangeNotification
                                                  object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMovieSourceTypeAvailableNotification
                                                  object:self.playerViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerReadyForDisplayDidChangeNotification
                                                  object:self.playerViewController.moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:self.playerViewController.moviePlayer];
}

AppDelegate.h

@property (strong, nonatomic) AFPlayerViewController *player;

AppDelegate.m

+ (AppDelegate *)shareInstance{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (UIViewController *)currentVisibleController{
    id rootController = self.window.rootViewController;

    if ([rootController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootController;
        return navigationController.topViewController;
    }

    if ([rootController isKindOfClass:[UITabBarController class]]) {

        UITabBarController *tabbarController = (UITabBarController *)rootController;
        id topViewController = [tabbarController.viewControllers objectAtIndex:tabbarController.selectedIndex];
        if ([topViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *navi = (UINavigationController *)topViewController;

            return navi.topViewController;
        }

        return topViewController;
    }
    return self.window.rootViewController;
}

当我想播放视频时:

[AppDelegate shareInstance].player = [[AFPlayerViewController alloc] initWithContentURL:link
                                                                            andSubtitlePath:subtitle
                                                                             withTypeServer:serverType
                                                                                   withName:nameFilm
                                                                              withEpisodeId:epObject.episodeId
                                                                                 withFilmId:filmId
                                                                               withDuration:lastDuration
                                                                                 withSeason:epObject.id_season];

[[AppDelegate shareInstance].currentVisibleController presentViewController:[AppDelegate shareInstance].player animated:NO completion:^{

}];

最佳答案

正如@Bannings 所说,您很可能会收到错误消息,因为通知被发送到已删除的 Controller 对象。尽管如此,我还有一些其他建议可以改进您的代码(而且我很确定会消除错误)。

首先,文档说您应该注册 MPMoviePlayerLoadStateDidChangeNotification 并使用 loadState 方法来确定电影何时准备好开始播放。我强烈建议您这样做。

其次,每次出现 View 时,您都在实例化一个 MPMoviePlayerController。我建议您创建一个 AFPlayerViewController 和一个 MPMoviePlayerController。每当您想播放另一部电影时,都显示相同的 AFPPlayerViewController

当您第一次实例化 MPMoviePlayerController 时,您需要一个 URL,用于它的 init 方法,但是可以使用 contentURL 启动后续电影> MPMoviePlayerController 的属性

我的建议的另一个好处是,用户可以很容易地暂停电影,转到不同的 View ,然后在返回电影 View 时快速恢复。

关于ios - 在 iOS 6 上开始播放视频时 MPMoviePlayerController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435818/

相关文章:

video - 如何在以 MPEG-TS 格式打包的 MPEG-2 流中查找关键帧

iOS HTTP Live Streaming - 当比特率未知时请求的字节范围?

ios - 使用 iOS 11.2 以编程方式滚动到 UITableView 顶部或第一个 TableViewCell 时出现问题

ios - swift 2 : Image picker crashes after at didFinishPickingMediaWithInfo

ios - 如何让应用程序从变量名运行方法

ios - 加载 View 时如何异步加载MPMoviePlayerController contentUrl?

ios - 删除/停用 MoviePlayerControl 上的全屏按钮

android - 如何创建一个使对象在 Corona 中移动的按钮

video - FFMPEG hwaccel 将 WEBM 转换为 MP4 视频失败 : could not find codec parameters

javascript - 在视野之外时暂停 HTML5 视频