我正在调整一个分叉的TouchBar应用程序,该应用程序在Mac TouchBar上显示小部件,并试图在NowPlaying小部件上显示相册艺术。这就是项目。
目前,我对这段代码有问题:
- (void)updateInfo
{
MRMediaRemoteGetNowPlayingInfo(dispatch_get_main_queue(),
^(NSDictionary *info)
{
NSString *album = [info objectForKey:kMRMediaRemoteNowPlayingInfoAlbum];
NSString *artist = [info objectForKey:kMRMediaRemoteNowPlayingInfoArtist];
NSString *title = [info objectForKey:kMRMediaRemoteNowPlayingInfoTitle];
NSData *artworkData = [info objectForKey:kMRMediaRemoteNowPlayingInfoArtworkData];
NSImage *albumart = nil;
if (nil != artworkData && ![artworkData isEqual:[NSNull null]])
{
albumart = [[NSImage alloc] initWithData:artworkData];
}
if (nil != albumart)
{
self.appIcon = albumart;
}
if (self.album != album || self.artist != artist || self.title != title || self.albumArt != albumart)
{
self.album = album;
self.artist = artist;
self.title = title;
self.albumArt = albumart;
[[NSNotificationCenter defaultCenter]
postNotificationName:NowPlayingInfoNotification
object:self];
}
});
}
我遇到的主要问题是,专辑艺术数据只在歌曲开始后的半秒左右出现,然后就消失了。我的理论是,kMRMediaRemoteNowPlayingInfoArtworkData用于获取艺术品数据是错误的,但我可能错了。
发现Spotify和Music在从通知中心提取时不会立即更新专辑艺术数据。解决方案是增加大约50ms的延迟。