Play Audio when device in silent mode - ios swift

前端 未结 8 1888
无人及你
无人及你 2020-12-23 16:58

I am creating an application using xcode 7.1, swift. I want to play an audio. Everything is fine. Now my problem I want to hear sound when the device in silent mode or muted

8条回答
  •  执笔经年
    2020-12-23 17:28

    You can go through this, it will help you out

    When you use following audio session categories, sounds will not be muted on iOS: AVAudioSessionCategoryPlayback,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayAndRecord

    Example

    func playSound (Sound: String, Type: String) {
    
            //Prepare the sound file name & extension
            var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(Sound, ofType: Type)!)
    
            //Preparation to play
            AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
            AVAudioSession.sharedInstance().setActive(true, error: nil)
    
            //Play audio
    
            var error: NSError?
            audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
            }
    

提交回复
热议问题