0

MPMoviePlayerViewController動画再生に使用しています。私のアプリでは、標準MPMoviePlayerControllerクラスを使用して、アプリ内のビデオを再生しています。iOS 7および8で正常に動作します

これは初めての場合はうまく機能しますが、1 つのビデオを見た後、他の何かを見ようとすると、アプリがMPMoviePlayerControllerの play メソッドでクラッシュし、次のエラーが表示されます。

: 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ビューが表示されるたびに をインスタンス化しています。AFPlayerViewControllersingleとsingle を作成することをお勧めしますMPMoviePlayerControllerAFPPlayerViewController別の映画を再生したいときはいつでも同じものを提示してください。

を最初にインスタンス化するときは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 に答える