【问题标题】:MPMoviePlayerController crash when start play a video on iOS 6在 iOS 6 上开始播放视频时 MPMoviePlayerController 崩溃
【发布时间】:2015-05-25 10:06:53
【问题描述】:

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

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

: CGContextSaveGState: 无效上下文 0x0

: CGContextClipToRect: 无效上下文 0x0

: CGContextTranslateCTM: 无效上下文 0x0

: CGContextDrawShading: 无效上下文 0x0

: CGContextRestoreGState: 无效上下文 0x0

*** -[MPMoviePlayerController retain]:消息发送到已释放的实例 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:^{

}];

【问题讨论】:

  • 可以添加回溯吗?当您的应用崩溃时,请在控制台中输入 bt。

标签: ios video mpmovieplayercontroller mpmoviewcontroller


【解决方案1】:

正如@Bannings 所说,由于通知被发送到已删除的控制器对象,您很可能会收到错误消息。不过,我还有一些其他建议可以改进您的代码(我相当肯定会消除错误)。

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

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

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

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

【讨论】:

  • 你能举个例子吗?
【解决方案2】:

如果您尝试观看其他内容,您是否致电removeNotification?尝试添加viewDidDisappear:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [self removeNotification];
}

【讨论】:

    【解决方案3】:

    我想我在这里看到了真正的问题:您应该使用MPMoviePlayerController 而不是MPMoviewPlayerViewController。进行更改,然后查看崩溃是否消失。我对loadState 的建议也应该遵循。

    【讨论】:

    • 是的,我尝试过使用 MPMoviePlayerController。但我仍然收到该消息:[MPMoviePlayerController retain]: message sent to deallocated instance。我想,我的 MPMoviewPlayerController 没有释放。
    猜你喜欢
    • 2014-10-04
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多