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