我正在使用AVPlayer播放MP3。 mp3也以后台模式播放。我想像其他任何媒体播放器应用程序那样从上拉通知面板访问音乐。
答案 0 :(得分:0)
使用MPNowPlayingInfoCenter
非常简单,首先阅读here
如果您使用的是backgroundMode
,那么您正在初始化AVAudioSession
,因此您只需将其添加到信息中心即可。
您可以按照blog
进行操作这就是我做的方式
MPNowPlayingInfoCenter* mpic = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *songDictionary = [[NSMutableDictionary alloc] init];
if (songInfo.detail.songTitle) {
[songDictionary setObject:songInfo.detail.songTitle forKey:MPMediaItemPropertyTitle];
} else {
[songDictionary setObject:songInfo.visibleName forKey:MPMediaItemPropertyTitle];
}
if (songInfo.detail.artist) {
[songDictionary setObject:songInfo.detail.artist forKey:MPMediaItemPropertyArtist];
}
if (songInfo.detail.album) {
[songDictionary setObject:songInfo.detail.album forKey:MPMediaItemPropertyAlbumTitle];
}
if (songInfo.detail.duration) {
double durationDouble = CMTimeGetSeconds([APPDELEGATE.session.playerItem duration]);
[songDictionary setObject:[NSNumber numberWithDouble:durationDouble] forKey:MPMediaItemPropertyPlaybackDuration];
double playbackDouble = CMTimeGetSeconds([APPDELEGATE.session.playerItem currentTime]);
[songDictionary setObject:[NSNumber numberWithDouble:playbackDouble] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
}
if(mpic) {
[mpic setNowPlayingInfo:songDictionary];
}
答案 1 :(得分:0)
我知道我在这篇文章上回答太迟了但是当我在搜索时我没有找到任何关于这个问题的解决方案。选中此项并添加您的代码,您的歌曲将显示在通知面板中。
//Add these lines where you are initializing your player and creating items for it.
_appdel.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_appdel.player currentItem]];
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlPlay)
{
NSLog(@"UIEventSubtypeRemoteControlPlay");
//[[AppMusicPlayer sharedService]playAudio];
[_appdel.player play];
}
else if (event.subtype == UIEventSubtypeRemoteControlPause)
{
NSLog(@"UIEventSubtypeRemoteControlPause");
//[[AppMusicPlayer sharedService]pauseAudio];
[_appdel.player pause];
}
else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
{
NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause");
}
else if (event.subtype == UIEventSubtypeRemoteControlNextTrack)
{
NSLog(@"UIEventSubtypeRemoteControlToggleNext");
}
else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack)
{
NSLog(@"UIEventSubtypeRemoteControlPrevious");
}
}
}
它在我身边工作,希望它能为你工作,谢谢......