我在swift 3中找到了从命令中心控制媒体的问题我在stackoverflow网络上找到了解决方案,但它不能让我控制视频轨道或来自后台的音频以下是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
let mpRemoteControlCenter = MPRemoteCommandCenter.shared()
mpRemoteControlCenter.nextTrackCommand.isEnabled = true
mpRemoteControlCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextTrack))
mpRemoteControlCenter.previousTrackCommand.isEnabled = true
mpRemoteControlCenter.previousTrackCommand.addTarget(self, action: #selector(self.prevTrack))
mpRemoteControlCenter.playCommand.isEnabled = true
mpRemoteControlCenter.playCommand.addTarget(self, action: #selector(self.play))
}
func play() {
print("play")
}
func nextTrack(){
print("next")
}
func prevTrack() {
print("prev")
}
非常感谢
答案 0 :(得分:1)
假设您正在使用Xcode 8,则需要在功能中打开背景模式,然后选中“音频,Airplay和画中画”。
将此代码添加到viewDidLoad()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch {
}
这样做之后应该有效。