我正在尝试使用MPNowPlayingInfoCenter更新标题。但是当我跳过轨道时,它不能在控制面板上反映它。
这是我的代码:
int newIndex = [(NSNumber *)(notification.userInfo)[DRNewTrackIndex] intValue];
NSUInteger trackCount;
if (self.station.sortOptionType == DRFilterPlaylistSortOptionTypeShuffle) {
trackCount = _station.playlist.tracks.count;
} else {
trackCount = _station.playlist.filteredTracks.count;
}
if (newIndex >= 0 && newIndex < trackCount) {
DRPlaylistTrackModel *track = (self.station.sortOptionType == DRFilterPlaylistSortOptionTypeShuffle)?(_station.playlist.tracks)[newIndex]:(_station.playlist.filteredTracks)[newIndex];
NSDictionary *nowPlayingInfoDictionary = [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo;
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] initWithDictionary:nowPlayingInfoDictionary];
DRDDLogVerbose(@"Setting nowPlayingInfo: (songName: %@, bandName: %@, albumName: %@)",
track.title, track.artist.name, track.album.name);
NSString *title = [self titleForTrack:track];
if (title.length > 0) {
songInfo[MPMediaItemPropertyTitle] = title;
}
NSString *subTitle = [self subTitleForTrack:track];
if (subTitle.length > 0) {
songInfo[MPMediaItemPropertyAlbumTitle] = subTitle;
}
if (title.length > 0 || subTitle.length > 0) {
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:[songInfo copy]];
} else {
// if title and station name are both empty, clear out old info
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
}
} else {
// clear old info
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
}
此外,当我将调试点放在标题和副标题附近时,我可以看到为下一首歌曲显示的正确信息。但是当我在手机上看到它时,它没有显示正确的跟踪信息,如调试点所示。 我不知道这里发生了什么。 我也保证上述内容仅在主线程上发生。