尽管这个问题问了很多次,但我没有找到满意的答案,但我还是想再试一次:
我想和MPMediaItems一起玩MPMusicPlayerController。到目前一切尚好。这是需要播放来自Itunes或Apple的文件。任何其他文件都是通过AVPlayer播放的。
使用以下方法处理使用AVPlayer播放的歌曲的远程控件似乎没有问题:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];和
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent此外,还可以在背景中检索AVPlayerItemDidPlayToEndTimeNotification,对此作出反应,开始播放下一首歌曲。
尝试所有这些与MPMusicPlayerController一起播放一首歌似乎不像它应该做的那样有效。我试着唱一首这样的歌:
self.audioPlayer = [MPMusicPlayerController applicationMusicPlayer];
self.audioPlayer.repeatMode = MPMusicRepeatModeNone;
[self.audioPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:@[_playerItem]]];利用给定的观察员:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackStateDidChange:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackItemDidChange:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
[self.audioPlayer beginGeneratingPlaybackNotifications];如果应用程序在前台,观察员就会被按他们应有的方式调用。作为背景,观察员不再被调用。在这种情况下,我不可能检测到歌曲的结尾跳到下一首歌。
同时,遥控器也不能工作。我用:
[MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)];
[MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remoteStop)];
[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand addTarget:self action:@selector(loadPreviousSong)];
[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(loadNextSong)];但是在前景和背景下,这些给定的选择器被称为。
有人能解决我的问题吗?如前所述,我需要使用MPMusicPlayerController,因为我想播放苹果音乐和iTunes匹配音轨。
相关问题:
发布于 2016-08-22 22:43:31
因为音乐应用程序正在处理项目的实际回放,所以您自己的应用程序没有收到通知。最好的方法是通过bugreport.apple.com提交一个增强请求。
Apple / iTunes团队正在听取关于如何从我在WWDC上得知的改进Apple的反馈意见。
https://stackoverflow.com/questions/38918616
复制相似问题