【发布时间】:2015-08-21 08:05:30
【问题描述】:
我正在使用 AVPlayer 播放 MP3。 mp3 也在后台模式下播放。我想像任何其他媒体播放器应用一样从上拉通知面板访问音乐。
【问题讨论】:
标签: ios objective-c avaudioplayer
我正在使用 AVPlayer 播放 MP3。 mp3 也在后台模式下播放。我想像任何其他媒体播放器应用一样从上拉通知面板访问音乐。
【问题讨论】:
标签: ios objective-c avaudioplayer
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];
}
【讨论】:
我知道我在这篇文章上回答为时已晚,但是当我搜索它时,我没有找到任何关于这个问题的解决方案。选中此项并添加您的代码,您的歌曲将显示在通知面板中。
//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");
}
}
}
它对我有用,希望它对你有用,谢谢..
【讨论】: