0

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

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

: CGContextSaveGState: 无效的上下文 0x0

: CGContextClipToRect: 无效的上下文 0x0

: CGContextTranslateCTM: 无效的上下文 0x0

: CGContextDrawShading: 无效的上下文 0x0

: CGContextRestoreGState: 无效的上下文 0x0

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

}];
4

3 回答 3

1

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

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

MPMoviePlayerController其次,每次您的视图出现时,您都在实例化 a 。我建议您创建一个单一的AFPlayerViewController和一个单一的MPMoviePlayerController. AFPPlayerViewController每当您想播放另一部电影时,呈现相同的内容。

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

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

于 2015-05-28T03:57:18.243 回答
0

removeNotification如果你试着看别的东西,你有没有打电话?尝试添加viewDidDisappear

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

    [self removeNotification];
}
于 2015-05-28T02:30:31.087 回答
0

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

于 2015-05-29T13:42:06.027 回答