AVPlayerItem被释放

时间:2018-07-14 13:54:49

标签: avplayer avplayeritem

请帮助我找到最佳解决方案,因为它开始令人讨厌。 我使用AVPlayer从URL播放广播,也使用AVPlayerItem获取播放器的状态和元数据标题。只是在iOS 10上出现了这种麻烦(在IOS 11上很好),当我快速按下PLAY / PAUSE按钮时,我的应用因崩溃而崩溃:

keyCode = modalIsOpen || e.keyCode

所以我的跟随代码播放:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x165c3830 of class AVPlayerItem was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x1669d390> (
<NSKeyValueObservance 0x165c48c0: Observer: 0x1659e1a0, Key path: timedMetadata, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x166b21c0>

暂停:

-(void)playStream{

    AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:self.urlFileMp3] options:nil];
    self.anItem = [AVPlayerItem playerItemWithAsset:asset];

    if (!self.audioController) {
        self.audioController = [[AVPlayer alloc] init];
    }
    [self.audioController replaceCurrentItemWithPlayerItem:self.anItem];
    [self.audioController addObserver:self forKeyPath:@"currentItem.loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];


    [self.anItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];

}

并获取播放器和元数据的状态:

-(void)stopStream{

    [self.audioController pause];

    [self.audioController replaceCurrentItemWithPlayerItem:nil];

   [self.anItem removeObserver:self forKeyPath:@"timedMetadata"];

}

PS:我不想使用@try @catch-这是非常糟糕的解决方案。

0 个答案:

没有答案