简体   繁体   English

使用 AudioKit 锁定屏幕中的音频控制

[英]Audio controls in lock screen with AudioKit

I am using AudioKit to manage my sound engine in my app.我正在使用 AudioKit 在我的应用程序中管理我的声音引擎。 I think AudioKit's settings are overriding something and I can't get lock screen audio controls.我认为 AudioKit 的设置覆盖了某些东西,我无法获得锁屏音频控件。 This is the code I have currently:这是我目前拥有的代码:

//Configure media control center
    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()

    commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the pause command
        return .success
    }

    commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the play command
        return .success
    }

I have this in a function which sets up all of my AudioKit settings but it doesn't work.我在一个函数中设置了我的所有 AudioKit 设置,但它不起作用。 Is there a special procedure for lock screen audio controls with AudioKit?是否有使用 AudioKit 进行锁屏音频控制的特殊程序?

you have to write these code in appdelegate to implement :你必须在 appdelegate 中编写这些代码才能实现:

do {
    try AVAudioSession.sharedInstance().setActive(true)
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}catch{

}
UIApplication.shared.beginReceivingRemoteControlEvents()
You have to rewrite this methord:
//MARK:-音乐播放相应后台状态
override func remoteControlReceived(with event: UIEvent?) {
    if (event?.type == .remoteControl) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: Configs.remotePlayCotrolNotificaation), object: nil, userInfo: ["event":event!])
    }
}

请记住,禁用.mixWithOthers很重要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM