2

我正在制作一个播放音频的应用程序,并且我已经对其进行了设置,以便通过 更新锁定屏幕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 调用。

任何想法可能导致异常?

4

2 回答 2

3

我觉得你打电话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.)

ps 我用 m4a 文件尝试了代码,发现durInterval是 NotANumber。使用正确的持续时间并且只调用setNowPlayingInfo一次,进度条跟踪良好并且没有崩溃。

于 2016-09-18T12:18:20.477 回答
0

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

于 2019-02-09T07:52:40.793 回答