在swift中以静音模式播放闹钟
如果我在后台音频,那么developer.apple.com发送给我邮件
您的应用声明在Info.plist中的UIBackgroundModes键中声明了对音频的支持,但是当应用在后台运行时,我们无法播放任何可听内容。
下一步
音频密钥旨在供在后台为用户提供可听内容的应用程序使用,例如音乐播放器或流式音频应用程序。 请修改您的应用,以便在应用处于后台时向用户提供可听内容,或从UIBackgroundModes键中删除“音频”设置。
代码: –
let appDelegate = UIApplication.shared.delegate as! AppDelegate if soundPath == "" { appDelegate.playSound() }else { appDelegate.playSoundWithPath(notificationSoundPath: soundPath) }
播放声音():-
func playSound() { let url = Bundle.main.url(forResource: "loud_alarm", withExtension: "caf")! do{ player = try AVAudioPlayer(contentsOf: url) guard let player = player else {return} player.prepareToPlay() player.play() } catch _ as NSError { } }
如何解决这个问题呢
在播放音频之前添加以下代码:
do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) } catch { // catch error }
我发送邮件developer.apple.com邮件背景音频失败
这个:-
准则2.5.4 – 性能 – 软件要求您的应用程序声明支持Info.plist中UIBackgroundModes键的音频,但不包括需要持久音频的function。
后续步骤音频密钥旨在供在后台为用户提供可听内容的应用程序使用,例如音乐播放器或流式音频应用程序。 请修改您的应用,以便在应用处于后台时向用户提供可听内容,或从UIBackgroundModes键中删除“音频”设置。
如何解决这个问题
我的代码是
trunOffAlarm.isHidden = false let appDelegate = UIApplication.shared.delegate as! AppDelegate if soundPath == "" { appDelegate.playSound() }else { appDelegate.playSoundWithPath(notificationSoundPath: soundPath) }
和playsound()函数代码是
func playSound() { let url = Bundle.main.url(forResource: "loud_alarm", withExtension: "caf")! do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) //print("AVAudioSession Category Playback OK") do { try AVAudioSession.sharedInstance().setActive(true) //print("AVAudioSession is Active") } catch _ as NSError { //print(error.localizedDescription) } } catch _ as NSError { //print(error.localizedDescription) } do { player = try AVAudioPlayer(contentsOf: url) guard let player = player else {return} player.prepareToPlay() player.play() } catch _ as NSError { }
/** setup and play the sound of the local mp3 file */ @objc func setupAudioPlayer() { // TODO: load the audio file asynchronously and observe player status if (userSelectedRingTone.isEmpty) { ringToneName = "Default_Siren" } else { guard (isFromHamburgerMenuDefaultTone) else { ringToneName = self.userSelectedRingTone isFromHamburgerMenuDefaultTone = false self.playSound(soundName: ringToneName) return } ringToneName = "Default_Siren" } self.playSound(soundName: ringToneName) } func playSound( soundName : String) { guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else { return } do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/ audioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue) audioPlayer?.delegate = self guard let player = audioPlayer else { return } // player.play() } catch let error { print(error.localizedDescription) } } // if you continue to play the audio then add this delegate method func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { if (playAudioRepeatedly) { audioPlayer?.play() } }