MPNowPlayingInfoCenter抛出EXC_BAD_ACCESS

时间:2016-09-18 08:01:18

标签: ios objective-c avfoundation mpnowplayinginfocenter

我正在创建一个播放音频的应用,我已将其设置为锁定屏幕通过MPNowPlayingInfoCenter更新,但我遇到了问题。

在看似随机的时候,我在尝试更新正在播放的信息时遇到EXC_BAD_ACCESS错误。

这是执行此操作的代码:

- (void)updatePlayback
{
    if(!active)
        return;

    NowPlayingController* npc = [AudioController nowPlayingController];
    CMTime elapsed = player.currentTime;
    Float64 elInterval = CMTimeGetSeconds(elapsed);
    [npc setElapsed:elInterval];

    CMTime duration = player.currentItem.duration;
    Float64 durInterval = CMTimeGetSeconds(duration);
    [npc setRemaining:ceilf(durInterval - elInterval)];

    [npc setPlayPauseValue:isPlaying];
    if(durInterval > 0)
    {
        [npc setProgressValue:elInterval/durInterval];
        [npc setAudioDuration:durInterval];
    }

    _activeMetadata[MPMediaItemPropertyPlaybackDuration] = @(durInterval);
    _activeMetadata[MPNowPlayingInfoPropertyPlaybackRate] = @(isPlaying);
    _activeMetadata[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(elInterval);

    MPNowPlayingInfoCenter* npInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
    if(npInfoCenter && _activeMetadata)
    {
        if([npInfoCenter respondsToSelector:@selector(setNowPlayingInfo:)])
        {

//////////THE FOLLOWING LINE TRIGGERS EXC_BAD_ACCESS SOMETIMES////////////
            [npInfoCenter setNowPlayingInfo:_activeMetadata];
        }

    }
}

99.9%的时间,这是有效的,但有时候将应用程序退出到后台或更改音频文件时,或者只是随机地,

[npInfoCenter setNowPlayingInfo:_activeMetadata];

抛出EXC_BAD_ACCESS

此外,_activeMetadata声明为:

@property (atomic, strong, retain) NSMutableDictionary* activeMetadata;

创建AVPlayer时实例化:

    AVAsset* asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:path]];
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
    player = [AVPlayer playerWithPlayerItem:playerItem];

    CMTime duration = player.currentItem.duration;
    NSTimeInterval durInterval = CMTimeGetSeconds(duration);
    NSLog(@"%f", durInterval);

    MPMediaItemArtwork* albumArtwork = [[MPMediaItemArtwork alloc] initWithImage:[downloader useCachedImage:CacheKeySeriesBanners withName:nil withURL:info[@"image"]]];
    NSDictionary* nowPlayingInfo = @{MPMediaItemPropertyTitle:ptString,
                                     MPMediaItemPropertyArtist:spString,
                                     MPMediaItemPropertyArtwork:albumArtwork,
                                     MPMediaItemPropertyAlbumTitle:info[@"title"],
                                     MPMediaItemPropertyPlaybackDuration:@(durInterval),
                                     MPNowPlayingInfoPropertyPlaybackRate:@(1),
                                     MPNowPlayingInfoPropertyElapsedPlaybackTime:@(0)};
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlayingInfo];

    _activeMetadata = [nowPlayingInfo mutableCopy];

updatePlayback在每一帧都通过CADisplayLink调用。

任何想法可能导致异常?

2 个答案:

答案 0 :(得分:2)

我认为你经常以setNowPlayingInfo的方式打电话。当然,它确实不应该崩溃,但是没有必要使用CADisplayLink每秒调用60次。

那你为什么这么经常打电话呢?如果是因为你想要顺利进行跟踪,那么仍然没有必要。来自MPNowPlayingInfoPropertyElapsedPlaybackTime声明:

// The elapsed time of the now playing item, in seconds.
// Note the elapsed time will be automatically extrapolated from the previously 
// provided elapsed time and playback rate, so updating this property frequently
// is not required (or recommended.)

P.S。我用m4a文件尝试了代码,发现durInterval是Notanumber。使用正确的持续时间并仅调用setNowPlayingInfo一次,进度条跟踪正常和什么都没有崩溃。

答案 1 :(得分:0)

Apple在iOS 10.3及更高版本中修复了此崩溃问题。 因此,如果要支持iOS 10.2.1及更低版本,请确保限制设置[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo属性的频率。也许每秒只能限制设置一次属性。