Ios 是否有使用AVAudioEngine和AVAudioPlayerNode显示锁屏控件的方法?
我在应用程序中使用AVAudioEngine和AVAudioPlayerNode处理音频播放,我想实现远程控制。后台音频已配置并正常工作 Control center控制工作,但当我从应用程序内部播放/暂停音乐时,播放/暂停按钮不会更新我正在实际设备上进行测试。 以下是我的AVAudioSession设置代码:Ios 是否有使用AVAudioEngine和AVAudioPlayerNode显示锁屏控件的方法?,ios,swift,avaudiosession,avaudioengine,mpremotecommandcenter,Ios,Swift,Avaudiosession,Avaudioengine,Mpremotecommandcenter,我在应用程序中使用AVAudioEngine和AVAudioPlayerNode处理音频播放,我想实现远程控制。后台音频已配置并正常工作 Control center控制工作,但当我从应用程序内部播放/暂停音乐时,播放/暂停按钮不会更新我正在实际设备上进行测试。 以下是我的AVAudioSession设置代码: func setupAudioSession() { UIApplication.shared.beginReceivingRemoteControlEvents()
func setupAudioSession() {
UIApplication.shared.beginReceivingRemoteControlEvents()
do {
try AVAudioSession.sharedInstance().setActive(true)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch let sessionError {
print("Failed to activate session:", sessionError)
}
}
MPRemoteCommandCenter设置:
func setupRemoteControl() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioPlayerNode.play()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioPlayerNode.pause()
return .success
}
}
func setupRemoteControl() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
try? self.audioEngine.start()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioEngine.stop()
return .success
}
}
锁屏控件-从未出现过。因此,这是我的问题的解决方案,我正在启动AVAudioEngine及其从
viewDidLoad()
调用的设置函数,这就是问题所在,我在AVAudioPlayerNode上使用了.play()
/.pause()
方法来操作音频,但是,AVAudioPlayerNode不发出主音频,AVAudioEngine的outputNode发出主音频
因此,无论何时您想从应用程序内部或从命令中心播放/暂停音频,如果您正在使用AVAudioEngine处理应用程序中的音频,请不要忘记在AVAudioEngine上调用.stop()
/.start()
方法。即使没有将单个属性设置为mpnowplayingfocenter.default().nowPlayingInfo
,也应显示锁屏控件,并且在命令中心/锁屏中正确更新播放/暂停按钮
MPRemoteCommandCenter设置:
func setupRemoteControl() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioPlayerNode.play()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioPlayerNode.pause()
return .success
}
}
func setupRemoteControl() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
try? self.audioEngine.start()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioEngine.stop()
return .success
}
}
@马特编辑了这本书question@matt是的,一切都在后台进行,一切都很好。已配置后台模式。也许我错过了一些AVAudioEngine的特殊设置?@matt当然,我可以从控制中心播放/暂停音频。但是如果我进入我的应用程序并暂停调用AVaudioPlayerNode.pause()的音频,control center中的播放按钮图标将不会从“pause icon”更新为“play icon”。例如,在Reverse中也会发生同样的情况请注意,如果您要多次访问play/pause命令,最好使用AVAudioEngine的pause()方法而不是停止()。谢谢!!!多年来我一直在想这个问题。我只是暂停了个别的音符,而不是整个引擎。