1. 程式人生 > >cocos2d-x 在ios端播放完mp4會崩潰的問題

cocos2d-x 在ios端播放完mp4會崩潰的問題

在 cocos2d/cocos/ui/UIVideoPlayer-ios.mm 檔案裡,增加一個方法:

- (void) deallocPlayer
{
    if (self.moviePlayer != nullptr) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];

        [self.moviePlayer stop];
        [self.moviePlayer.view removeFromSuperview];
        self.moviePlayer = nullptr;
        _videoPlayer = nullptr;
    }
}

找到 dealloc 方法,註釋掉全部程式碼,只保留 [super dealloc];

-(void) dealloc
{
//    if (self.moviePlayer != nullptr) {
//        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
//        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
//
//        [self.moviePlayer stop];
//        [self.moviePlayer.view removeFromSuperview];
//        self.moviePlayer = nullptr;
//        _videoPlayer = nullptr;
//    }
    [super dealloc];
}

修改VideoPlayer的建構函式,增加retain,解構函式,增加release:

VideoPlayer::VideoPlayer()
: _isPlaying(false)
, _fullScreenDirty(false)
, _fullScreenEnabled(false)
, _keepAspectRatioEnabled(false)
, _videoPlayerIndex(-1)
, _eventCallback(nullptr)
{
    _videoView = [[[UIVideoViewWrapperIos alloc] init:this] retain];
//    _videoView = [[UIVideoViewWrapperIos alloc] init:this];

#if CC_VIDEOPLAYER_DEBUG_DRAW
    _debugDrawNode = DrawNode::create();
    addChild(_debugDrawNode);
#endif
}
VideoPlayer::~VideoPlayer()
{
    if(_videoView)
    {
         [((UIVideoViewWrapperIos*)_videoView) deallocPlayer];
        [((UIVideoViewWrapperIos*)_videoView) release];
//        [((UIVideoViewWrapperIos*)_videoView) dealloc];
    }
}

結束~