快速以静音模式播放警报

Posted

技术标签:

【中文标题】快速以静音模式播放警报【英文标题】:Play Alarm in silent mode in swift 【发布时间】:2017-11-06 06:14:20 【问题描述】:

如果我使用背景音频,那么 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 

    

如何解决这个问题

【问题讨论】:

【参考方案1】:

在播放音频之前添加以下代码:

do 
    try AVAudiosession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
 
 catch 
    // catch error
 

【讨论】:

【参考方案2】:

我为后台音频发送邮件 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)
        

而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 

    

【讨论】:

【参考方案3】:
/**
 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()
    

【讨论】:

以上是关于快速以静音模式播放警报的主要内容,如果未能解决你的问题,请参考以下文章

覆盖iOS系统音量以播放警报

iPhone静音时的UILocalNotification警报

克服锁屏+静音模式播放音频

iOS:静音模式下无法播放我的声音

iOS - 无法在后台播放声音

iPhone静音时如何播放声音?