MPMusicPlayerController

MPMusicPlayerController

我的应用使用MPMusicPlayerController.systemMusicPlayer()播放音频,效果很好。

用户可以设置自定义currentPlaybackRate。这按预期工作。

如果用户在锁定屏幕上按一个操作(MPRemoteCommandCenter),则currentPlaybackRate重置为1。这是因为事件直接发送到系统音乐播放器,而不是应用程序控制器。

要将currentPlaybackRate设置为适当的值,我尝试覆盖MPRemoteCommandCenter事件:

override func viewDidLoad() {
    super.viewDidLoad()

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "willEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playbackStateChanged:", name: MPMusicPlayerControllerPlaybackStateDidChangeNotification, object: player)
    player.beginGeneratingPlaybackNotifications()

    let rcc : MPRemoteCommandCenter = MPRemoteCommandCenter.sharedCommandCenter()
    rcc.playCommand.addTargetWithHandler { (event: MPRemoteCommandEvent!) -> MPRemoteCommandHandlerStatus in
        NSLog("Lock Screen Play Pressed")
        return MPRemoteCommandHandlerStatus.Success
    }
    rcc.togglePlayPauseCommand.addTargetWithHandler { (event: MPRemoteCommandEvent!) -> MPRemoteCommandHandlerStatus in
        NSLog("Lock Screen Play/Pause Pressed")
        return MPRemoteCommandHandlerStatus.Success
    }
}

使用MPRemoteCommandCenteraddTargetWithHandler是否应该尊重MPMusicPlayerController吗?

最佳答案

如果您确实不需要MPMusicPlayerController,请尝试使用此解决方案
https://github.com/gangverk/GVMusicPlayerController
这个家伙结合了AVAudioPlayer和MPMusicPlayerController的行为。

但我需要答案。因为在我的应用程序中,我确实需要MPMusicPlayerController。

10-08 05:52