AVPlayer was deallocated while key value observers were still registered with it

前端 未结 5 1398
滥情空心
滥情空心 2021-01-31 08:45

I am creating a simple media player app. My App is crashed when first link is played and I clicked second link in uitableview.

- (void)viewDidLoad {
        [s         


        
5条回答
  •  悲&欢浪女
    2021-01-31 09:07

    I had a similar problem. It worked fine in iOS 7, and now it crashes in iOS 8.

    The solution was to remove the observer, before releasing the object.

    When you replace or allocate a new object for a member, you're releasing the old object, so you need to remove the observer first :

    -(void) setupAVPlayerForURL: (NSURL*) url1 {
        AVAsset *asset = [AVURLAsset URLAssetWithURL:url1 options:nil];
        AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
        if (player != nil)
            [player removeObserver:self forKeyPath:@"status"];
        player = [AVPlayer playerWithPlayerItem:anItem]; 
        [player addObserver:self forKeyPath:@"status" options:0 context:nil];
    }
    

    And similarly in btnPlayClick ( in case it is pressed without btnStop_Click being pressed) :

    - (IBAction)btnPlay_Click:(id)sender {
         if (player != nil && [player currentItem] != nil)
             [[player currentItem] removeObserver:self forKeyPath:@"timedMetadata"];
        AVPlayerItem *item = player.currentItem;
        [item addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionInitial|     NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld| NSKeyValueObservingOptionPrior context:nil];
        [player play];
    }
    

提交回复
热议问题