         }
     }
 
+    private func setupAudioSession () {
+
+        var error : NSError?;
+        let auSession = AVAudioSession.sharedInstance()
+        auSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker, error: &error)
+        if error != nil {
+            println("Error when setting up audio session")
+            println(error)
+        }
+
+        auSession.setMode(AVAudioSessionModeVoiceChat, error: &error)
+        if error != nil {
+            println("Error when setting up audio session")
+            println(error)
+        }
+
+        auSession.setActive(true, withOptions: AVAudioSessionSetActiveOptions.OptionNotifyOthersOnDeactivation, error: &error)
+        if error != nil {
+            println("Error when setting up audio session")
+            println(error)
+        }
+
+        auSession.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: &error)
+        if error != nil {
+            println("Error when setting up audio session")
+            println(error)
+        }
+
+    }
+
+    private func stopAudioSession () {
+        AVAudioSession.sharedInstance().setActive(false, error: nil)
+    }
 }

    var audioPlayer = AVAudioPlayerNode()
    func decodeAndPlay(data: NSData) {
        let buffer = AVAudioPCMBuffer()

        audioPlayer.scheduleBuffer(buffer, completionHandler: {})
    }
}



    init() {

        var error : NSError?;

        setupAudioSession()

        engine.connect(engine.mainMixerNode, to: engine.outputNode, format: nil)
        engine.startAndReturnError(&error)

        if error != nil {
            println("Error when running engine")
            println(error)
        }
    }

    deinit {
        engine.stop()
        stopAudioSession()
    }

    public func startRecord(block: CompressedDataClosure) {
