2014-10-08 75 views
25

我正在创建一个简单的媒体播放器应用程序。我的应用程序在第一个链接播放时崩溃,并在uitableview中点击第二个链接。AVPlayer被释放,而关键值观察员仍然注册了它

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     arrURL = [NSArray arrayWithObjects: @"http://yp.shoutcast.com/sbin/tunein-station.pls?id=148820", @"http://www.kcrw.com/pls/kcrwmusic.pls",@"http://yp.shoutcast.com/sbin/tunein-station.pls?id=175821",@"http://yp.shoutcast.com/sbin/tunein-station.pls?id=148820",@"http://yp.shoutcast.com/sbin/tunein-station.pls?id=70931",nil]; 
     url = [[NSURL alloc] init];  
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

     return [arrURL count]; 
    } 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ; 
    } 
    cell.textLabel.text = [arrURL objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedSongIndex = indexPath.row; 
    url = [[NSURL alloc] initWithString:[arrURL objectAtIndex:indexPath.row]]; 
    [self setupAVPlayerForURL:url]; 
    [player play]; 

    //[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
- (IBAction)btnPlay_Click:(id)sender { 

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

    [player pause]; 
} 

- (IBAction)btnStop_Click:(id)sender { 

    [player pause]; 
} 
-(void) setupAVPlayerForURL: (NSURL*) url1 { 
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url1 options:nil]; 
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset]; 

    player = [AVPlayer playerWithPlayerItem:anItem]; **//Application Crashed** 
    [player addObserver:self forKeyPath:@"status" options:0 context:nil]; 
} 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if([keyPath isEqualToString:@"timedMetadata"]) 
    { 
     AVPlayerItem *item = (AVPlayerItem *)object; 
     NSLog(@"Item.timedMetadata: %@",item.timedMetadata); 
     NSLog(@"-- META DATA ---"); 
     //  AVPlayerItem *pItem = (AVPlayerItem *)object; 
     for (AVMetadataItem *metaItem in item.timedMetadata) { 
      NSLog(@"meta data = %@",[metaItem commonKey]); 
      NSString *key = [metaItem commonKey]; //key = publisher , key = title 
      NSString *value = [metaItem stringValue]; 
      NSLog(@"key = %@, value = %@", key, value); 
      if([[metaItem commonKey] isEqualToString:@"title"]) 
      { 
       self.lblTitle.text = [metaItem stringValue]; 
      } 
     } 
    } 
    if (object == player && [keyPath isEqualToString:@"status"]) { 
     if (player.status == AVPlayerStatusFailed) { 
      NSLog(@"AVPlayer Failed"); 
     } else if (player.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"AVPlayer Ready to Play"); 
     } else if (player.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"AVPlayer Unknown"); 
     } 
    } 
} 

当应用程序崩溃时,我收到了此消息。

***终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,原因:“一个实例 类AVPlayer的0x165297c0被释放,而键值观察家仍然 其注册。现有的观测信息: (背景:为0x0,物业:0x1661d5d0>)”

应用程序崩溃只在IOS 8 IOS 7工作正常。 我在做什么错?

回答

45

我有类似的问题。它在iOS 7中运行良好,现在它在iOS 8中崩溃。

解决方案是在释放对象之前删除观察者。

当更换或分配一个新对象的一员,你释放旧的对象,所以你需要先删除观察者:

-(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]; 
} 
在btnPlayClick

而且类似(如果它被压而不按btnStop_Click):

- (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]; 
} 
+0

我把这个如果(球员=零!) { 如果(player.currentItem =零!) { [player.currentItem removeObserver:自forKeyPath:@ “状态”]; } }而不是if(player!= nil) [player removeObserver:self forKeyPath:@“status”];并为我工作很好。 – 2014-11-21 05:26:05

+0

嗨,大家好,我也遇到了这个问题,正在寻找最近2天的解决方案。请帮我解决这个问题。 我在这里发布了问题 - http://stackoverflow.com/questions/27838356/avplayeritem-was-deallocated-while-key-value-observers-were-still-registered-wit – 2015-01-08 14:36:44

+1

我实现你的代码,但得到错误 因未捕获异常'NSRangeException'而终止应用,原因:'无法从中删除关键路径“状态”的观察者,因为它未注册为观察者。 – Hardik 2015-01-09 04:16:29

2

使用KVO时,您必须在addObserver:forKeyPath:options:context:之间拨打电话removeObserver:forKeyPath:(请参阅KVO programming guide)以平衡呼叫。

尝试在停止按钮被点击时,将视图控制器作为观察者移除,例如,

- (IBAction)btnStop_Click:(id)sender { 
    [[player currentItem] removeObserver:self forKeyPath:@"timedMetadata"]; 
} 
+0

应用程序崩溃。 Crash Log:'无法从中删除关键路径“timedMetadata”的观察者,因为它没有注册为观察者。应用程序仅在IOS 8设备中崩溃。 – 2014-10-08 12:57:44

+1

mmcombe是正确的,除了错误指的是AVPlayer的状态'KVO。只需将他的答案更改为[[player] removeObserver:self forKeyPath:@“status”]; – MDB983 2014-10-08 14:37:39

+0

我的应用程序在IOS 8中崩溃,以下是我的崩溃日志:“终止应用程序,由于未捕获异常'NSInternalInconsistencyException',原因:'AVPlayerItem类的实例0x15edf250被释放,而键值观察者仍在其中注册。 – 2014-11-11 13:03:46

1
-(void)viewWillDisappear:(BOOL)animated 
{ 
[self.player removeObserver:self forKeyPath:@"status" context:nil]; 
} 
+0

你能提供围绕您的解决方案一点背景知识? – wogsland 2017-02-16 13:08:26

+1

虽然这个代码片段是受欢迎的,并且可能会提供一些帮助,但它会[如果包含解释](/ meta.stackexchange.com/q/114762)的* how *和* why *会大大改进,这将解决问题。请记住,你正在为将来的读者回答这个问题,而不仅仅是现在问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。 – 2017-02-16 13:30:20

0

使用AVPlayer的时候我也满足了类似的问题,崩溃日志信息说:

AVKeyPathFlattener类的实例0x174034600被解除分配,而键值观察者仍在其中注册。现有的观测信息:(背景:为0x0,物业:0x17405d6d0>)

至于什么苹果推荐,我本来是增加观测后初始化我AVPlayerItem对象,并在观察者的dealloc方法去除观测。因为我的观察者类在我的AVPlayerItem对象上保留了很强的引用,所以在我的观察者对象被释放之前不应该释放它。我真的不知道为什么会发生这种情况。

所以我试着用BlocksKit解决了这个问题,它现在对我来说工作的很好。

相关问题