我有同样的问题,现在找出你唯一需要做的事情
try? AVAudioSession.sharedInstance().setCategory(.playback)
一开始。不要使用.duckOthers
,.mixWithOthers
将目标添加到 RemoteCommand
func addRemoteCommandCenter() {
let rcc = MPRemoteCommandCenter.shared()
//添加暂停监听
rcc.pauseCommand.addTarget(self, action: #selector(playOrPauseEvent(_:)))
//添加播放监听
rcc.playCommand.addTarget(self, action: #selector(playOrPauseEvent(_:)))
//下一首
rcc.nextTrackCommand.addTarget(self, action: #selector(nextCommandEvent(_:)))
//上一首
rcc.previousTrackCommand.addTarget(self, action: #selector(previousCammndEvent(_:)))
//耳机暂停和播放的监听
rcc.togglePlayPauseCommand.addTarget(self, action: #selector(togglePlayPauseCommand(_:)))
}
并更新speechSynthesizer(_:didStart:)
UIspeechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)
let infoCenter = MPNowPlayingInfoCenter.default()
infoCenter.nowPlayingInfo = [MPMediaItemPropertyTitle:"Title", MPMediaItemPropertyArtist: "Artist", MPMediaItemPropertyAlbumTitle: "", MPMediaItemPropertyArtwork: MPMediaItemArtwork(boundsSize: CGSize(width: 120, height: 120), requestHandler: { (size) -> UIImage in
UIImage()
})]