Ios 在swift的静音模式下播放警报
如果我使用后台音频,则developer.apple.com向我发送邮件 您的应用程序声明支持Info.plist中的UIBackgroundModes键中的音频,但当应用程序在后台运行时,我们无法播放任何音频内容 下一步 音频键用于在后台向用户提供音频内容的应用程序,如音乐播放器或流媒体音频应用程序。请修改应用程序,以便在应用程序处于后台时向用户提供音频内容,或从UIBackgroundModes键中删除“音频”设置 代码:-Ios 在swift的静音模式下播放警报,ios,swift,alarm,Ios,Swift,Alarm,如果我使用后台音频,则developer.apple.com向我发送邮件 您的应用程序声明支持Info.plist中的UIBackgroundModes键中的音频,但当应用程序在后台运行时,我们无法播放任何音频内容 下一步 音频键用于在后台向用户提供音频内容的应用程序,如音乐播放器或流媒体音频应用程序。请修改应用程序,以便在应用程序处于后台时向用户提供音频内容,或从UIBackgroundModes键中删除“音频”设置 代码:- let appDelegate = UIApplication.s
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if soundPath == "" {
appDelegate.playSound()
}else {
appDelegate.playSoundWithPath(notificationSoundPath: soundPath)
}
播放声音():-
如何解决此问题播放音频前添加以下代码:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
// catch error
}
我已向mail developer.apple.com发送后台音频邮件失败 这:- 准则2.5.4-性能-软件要求 您的应用程序在Info.plist的UIBackgroundModes键中声明支持音频,但不包括需要持久音频的功能 下一步 音频键供在后台向用户提供音频内容的应用程序使用, 例如音乐播放器或流媒体音频应用程序。 请修改应用程序,以便在应用程序处于后台时向用户提供音频内容,或从UIBackgroundModes键中删除“音频”设置 如何解决这个问题 我的代码是
trunOffAlarm.isHidden = false
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 {
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 {
}
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()
}
}