And*_*zon 5 iphone objective-c ios
我正在开发一种在后台播放音乐的音乐播放器.
该应用程序与Spotify和Apple音乐集成,用户只能在其中一项服务中进行身份验证.
目前,我可以在应用程序和后台播放两种服务的音乐.我也能够设置MPNowPlayingInfoCenter并显示正确的信息,但播放/暂停,下一首曲目和之前的曲目仅在用户使用Spotify进行身份验证时才有效.
当用户与Apple Music服务同步并尝试使用任何关闭命令时,应用程序无法接收通知,而Apple Music应用程序似乎正在处理它,从而播放Apple Music播放列表中的下一首歌曲而不是我的应用播放列表.
当我AVPlayer与Apple Music SPTAudioStreamingController同步并与Spotify同步时,我正在使用它来播放音乐.
我还验证了matt在这个问题上提出的要求:xcode - 在iOS 8上不显示MPNowPlayingInfoCenter信息
以下是媒体中心设置的代码:
- (void)setMediaCenterinfoForPlayer:(id)player {
SPTAudioStreamingController *spotifyPlayer;
AVPlayer *localPlayer;
NSMutableDictionary *trackInfo = [[NSMutableDictionary alloc] initWithDictionary: @{ MPMediaItemPropertyTitle: self.currentTrack.name,
MPMediaItemPropertyArtist: ((SPTArtist *)self.currentTrack.artists[0]).name,
MPMediaItemPropertyAlbumTitle : self.currentTrack.album.name,
MPNowPlayingInfoPropertyPlaybackRate: @(1.0)
}];
if ([player isKindOfClass:[SPTAudioStreamingController class]]) {
spotifyPlayer = (SPTAudioStreamingController *)player;
[trackInfo setObject:[NSNumber numberWithFloat:spotifyPlayer.currentPlaybackPosition] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[trackInfo setObject:[NSNumber numberWithFloat:spotifyPlayer.currentTrackDuration] forKey:MPMediaItemPropertyPlaybackDuration];
} else {
localPlayer = (AVPlayer *)player;
NSTimeInterval playbackTime = [self currentPlaybackTimeForPlayer:player];
[trackInfo setObject:@(playbackTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[trackInfo setObject:@(CMTimeGetSeconds(localPlayer.currentItem.asset.duration)) forKey:MPMediaItemPropertyPlaybackDuration];
}
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = trackInfo;
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
[self albumURLCoverForCurrentTrackWithBlock:^(UIImage *albumImage) {
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumImage];
[trackInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:trackInfo];
}];
}
}
Run Code Online (Sandbox Code Playgroud)
以下是事件处理的代码:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];
[playingInfo setObject:[NSNumber numberWithFloat:[AudioPlayerManager sharedInstance].spotifyPlayer.currentPlaybackPosition] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
center.nowPlayingInfo = playingInfo;
if (event.subtype == UIEventSubtypeRemoteControlPlay) {
[[AudioPlayerManager sharedInstance] playTrack];
} else if (event.subtype == UIEventSubtypeRemoteControlPause) {
[[AudioPlayerManager sharedInstance] pauseTrack];
} else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) {
[[AudioPlayerManager sharedInstance] previousTrack];
} else if (event.subtype == UIEventSubtypeRemoteControlNextTrack) {
[[AudioPlayerManager sharedInstance] nextTrack];
}
[[NSNotificationCenter defaultCenter] postNotificationName:kEventTypeRemoteControlUpdateState object:self];
}
}
Run Code Online (Sandbox Code Playgroud)
希望有人能指出我解决这种情况的方法.谢谢!
| 归档时间: |
|
| 查看次数: |
224 次 |
| 最近记录: |