-2

if i am on background audio then developer.apple.com send to me mail

Your app declares support for audio in the UIBackgroundModes key in your Info.plist, but we were unable to play any audible content when the app was running in the background.

Next Steps

The audio key is intended for use by apps that provide audible content to the user while in the background, such as music player or streaming audio apps. Please revise your app to provide audible content to the user while the app is in the background or remove the "audio" setting from the UIBackgroundModes key.

Code :-

let appDelegate = UIApplication.shared.delegate as! AppDelegate
if soundPath == "" {
   appDelegate.playSound()
}else {
   appDelegate.playSoundWithPath(notificationSoundPath: soundPath)
}

Play Sound():-

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 {

    }
}

how to solve this problem

Midhun MP
  • 103,496
  • 31
  • 153
  • 200

3 Answers3

1

Add below code before you play audio:

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
 }
 catch {
    // catch error
 }
iVarun
  • 6,496
  • 2
  • 26
  • 34
0

I have send mail developer.apple.com mail for background audio failed

this:-

Guideline 2.5.4 - Performance - Software Requirements Your app declares support for audio in the UIBackgroundModes key in your Info.plist but did not include features that require persistent audio.

Next Steps The audio key is intended for use by apps that provide audible content to the user while in the background, such as music player or streaming audio apps. Please revise your app to provide audible content to the user while the app is in the background or remove the "audio" setting from the UIBackgroundModes key.

How to solve this issue

and my code is

 trunOffAlarm.isHidden = false
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        if soundPath == "" {

            appDelegate.playSound()
        }else {
            appDelegate.playSoundWithPath(notificationSoundPath: soundPath)
        }

and playsound() function code is

 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 {

    }
-1
/**
 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()
    }
}
Rahul
  • 15
  • 5