ios - 锁屏上的 AVAudioPlayer

标签 ios avaudioplayer avaudiosession background-audio mpnowplayinginfocenter

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

- (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. 此方法仅启用锁定屏幕上的快退播放/暂停快进 按钮,但不会t 显示文件标题、插图和持续时间。我如何使用 AVAudioPlayerAVAudioSession 显示该信息(我真的不想再使用一个库/API 来实现它)?

    2-a。我在搜索时发现了MPNowPlayingInfoCenter,但我对它了解不多。我必须用它来实现上面的那些东西吗? :-[

最佳答案

你是对的,MPNowPlayingInfoCenter是做到这一点的唯一方法。所以继续链接MediaPlayer.framework .在处理播放轨道的类中,导入 <MediaPlayer/MediaPlayer.h> .每当您的轨道发生变化时,请执行以下操作:

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

    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;

关于ios - 锁屏上的 AVAudioPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363058/

相关文章:

ios - 什么是在 iOS 上使用 CSS 正确使用高分辨率 Sprite ?

ios - 禁用 UITableView float 部分标题点击?

ios - 如何在 AVQueuePlayer 中循环播放声音队列?

iphone - 移动音频播放器 5 秒快退和快进

iphone - 无法在 iOS 中获取音频输出

ios - 无法控制 iPad 上的扬声器

ios - Swift:使用 snapkit 时如何获得正确的 UICollectionView 高度?

ios - Xcode 组织者 : Can't Remove Device

ios - AVAudioSession 密码锁在 iOS8 上停止 AVAudioRecorder

ios - 当我打开我的应用程序时 iTunes 音乐停止