锁定屏幕上的AVAudioPlayer

时间:2013-12-03 22:30:32

标签: ios avaudioplayer avaudiosession background-audio mpnowplayinginfocenter

我使用 AVAudioPlayer (不是 AVPlayer )实现了音频播放器。我可以使用以下方法处理远程控制事件。到目前为止它工作得很好,但是我发现这些事件还有两个subtypesUIEventSubtypeRemoteControlEndSeekingForwardUIEventSubtypeRemoteControlEndSeekingBackward

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl)
    {
        if (event.subtype == UIEventSubtypeRemoteControlPlay)
        {
            [self playAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlPause)
        {
            [self pauseAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
        {
            [self togglePlayPause];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward)
        {
            [self rewindTheAudio]; //this method rewinds the audio by 15 seconds.
        }
        else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward)
        {
            [self fastForwardTheAudio]; //this method fast-forwards the audio by 15 seconds.
        }

}

所以问题:

  1. 为了让事情正常,我是否应该实施这两种子类型?

  2. 此方法仅启用锁定屏幕上的rewindplay/pausefast forward按钮,但不会显示文件标题,图稿和持续时间。如何使用AVAudioPlayerAVAudioSession显示该信息(我真的不想再使用一个库/ API来实现此功能)?

    2-A。我在搜索时发现了MPNowPlayingInfoCenter,我对此并不了解。我是否必须使用它来实现上述内容? : - [

1 个答案:

答案 0 :(得分:10)

你是对的,MPNowPlayingInfoCenter是唯一的方法。所以继续并与MediaPlayer.framework联系。在处理播放曲目的类中,导入<MediaPlayer/MediaPlayer.h>。每当您的曲目发生变化时,请执行以下操作:

NSDictionary *info = @{ MPMediaItemPropertyArtist: artistName,
                            MPMediaItemPropertyAlbumTitle: albumName,
                            MPMediaItemPropertyTitle: songName };

    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;