在 swift 中以静音模式播放闹钟

Play Alarm in silent mode in swift

如果我在播放背景音频,那么 developer.apple.com 给我发邮件

您的应用在 Info.plist 的 UIBackgroundModes 键中声明支持音频,但是当应用 运行 在后台时我们无法播放任何音频内容。

后续步骤

音频键专供在后台向用户提供音频内容的应用使用,例如音乐播放器或流式音频应用。请修改您的应用程序以在应用程序处于后台时向用户提供音频内容,或者从 UIBackgroundModes 键中删除 "audio" 设置。

代码:-

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 键中声明了对音频的支持,但不包含需要持久音频的功能。

后续步骤 音频密钥旨在供在后台向用户提供音频内容的应用程序使用, 例如音乐播放器或流式音频应用程序。 请修改您的应用程序以在应用程序处于后台时向用户提供声音内容,或者从 UIBackgroundModes 键中删除 "audio" 设置。

如何解决这个问题

我的代码是

 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()
    }
}