我正在使用AVPlayer来重现音乐。所有在iOS 6+上的在模拟器上工作正常。但是当我在设备(iPhone 5s,iOS 7.1)上运行它时,MPNowPlayingInfoCenter会显示来自任何其他音乐应用的信息:spotify / sound cloud / music player,而不是我应用中的当前播放项目。
以下是我用于在MPNowPlayingInfoCenter中设置音乐信息的代码:
- (void)setupNowPlayingInfoCenter
{
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
// Here enters fine.
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
if (self.playingUrl) {
NSMutableDictionary *songInfo = self.songInfo;
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[self.playingObject valueForKey:@"image"]];
[songInfo setObject:[self.playingObject valueForKey:@"song"] forKey:MPMediaItemPropertyTitle];
[songInfo setObject:[self.playingObject valueForKey:@"artist"] forKey:MPMediaItemPropertyArtist];
[songInfo setObject:[self.playingObject valueForKey:@"album"] forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:[NSNumber numberWithDouble:self.progress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[songInfo setObject:[NSNumber numberWithDouble:self.duration] forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:(self.isPaused ? @(0.0f) : @(1.0f)) forKey:MPNowPlayingInfoPropertyPlaybackRate];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
playingInfoCenter.nowPlayingInfo = songInfo;
}
其他相关代码:
- (NSError *)initialize
{
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback
error:&error];
if (error) {
NSLog(@"%@", [error description]);
return error;
}
[audioSession setActive:YES error:&error];
if (error) {
NSLog(@"%@", [_error description]);
return error;
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
return error;
}
非常感谢任何帮助。
答案 0 :(得分:3)
好吧,我自己找到了答案。
我正在创作每首歌的新播放器,如下:
self.audioPlayer = [AVPlayer playerWithURL:url];
我改为:
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
[self.audioPlayer replaceCurrentItemWithPlayerItem:playerItem];
并且神奇地开始工作。