我正在制作一个播放音频的应用程序,我已经对其进行了设置,以便通过 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 respondsToSelectorselector(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 调用。
任何想法可能导致异常?
Best Answer-推荐答案 strong>
我认为您过于频繁地调用 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.)
附言我用 m4a 文件尝试了代码,发现 durInterval 是 NotANumber。使用正确的持续时间并仅调用一次 setNowPlayingInfo ,进度条跟踪良好且没有崩溃。
关于ios - MPNowPlayingInfoCenter 抛出 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39555416/
|