Rami Ammoun Rami Ammoun - 8 months ago 53
Swift Question

how do I set my Audio Session Default Behavior to Playback in swift?

My app has a "Click" sound functionality. I used the

import AVFoundation

then the following function to run the "Click" sound:

var audioPlayer = AVAudioPlayer()

func playSound() {
var soundPath = NSBundle.mainBundle().pathForResource("tick", ofType: "wav")
var soundURL = NSURL.fileURLWithPath(soundPath!)
self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)

Now if the user is running a music player, my app causes the music player to stop. I read about the Audio Session Default Behavior in the documentation, but I don't know how to apply it.

Can you please help?

Thank you!


If you are wondering the syntax for swift 2, here it is:

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .DuckOthers)
    } catch {
        print("AVAudioSession cannot be set: \(error)")