如何在ios中将媒体控制器放置在上拉通知面板上

如何在ios中将媒体控制器放置在上拉通知面板上,ios,objective-c,avaudioplayer,Ios,Objective C,Avaudioplayer,我正在用AVPlayer播放MP3。mp3也在后台模式下播放。我想像任何其他媒体播放器应用程序一样,从上拉通知面板访问音乐。使用起来非常简单mpnowplayingfocenter,首先阅读 如果您使用的是backgroundMode您正在初始化一个AVAudioSession,因此您可以将它添加到信息中心 你可以照这个做 我就是这样做的 MPNowPlayingInfoCenter* mpic = [MPNowPlayingInfoCenter defaultCenter]; NSMu

我正在用AVPlayer播放MP3。mp3也在后台模式下播放。我想像任何其他媒体播放器应用程序一样,从上拉通知面板访问音乐。

使用起来非常简单
mpnowplayingfocenter
,首先阅读

如果您使用的是
backgroundMode
您正在初始化一个
AVAudioSession
,因此您可以将它添加到信息中心

你可以照这个做

我就是这样做的

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");

    }
}
}

这对我来说很有用,希望对你有用。谢谢。

是的。这很容易。在你发布之前从谷歌得到了答案。谢谢。