0

When I call the function .stopfetchingaudio() from EZAudio, my app crashes.

var microphone: EZMicrophone! 

func didMove(to view: SKView){

 /*
         * setup all dependencys for the fft analysis
         */

        //setup audio session
        session = AVAudioSession.sharedInstance()
        do{
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try session.setActive(true)
        }catch{
            print("Audio Session setup Fails")
        }

        //create a mic instance
        microphone = EZMicrophone(delegate: self)


}

func stopMic(){
    microphone.stopFetchingAudio()
}

I get this error:

xyz-abv[435:35687] fatal error: unexpectedly found nil while unwrapping an Optional value

But I don't know which optional it mean.

Bista
  • 7,869
  • 3
  • 27
  • 55
Aron
  • 1,179
  • 15
  • 29
  • Where is your microphone defined? – Bista Sep 19 '16 at 04:22
  • It is in a GameScene from SpriteKit defined. The idea is when i switch to a other view controller i will stop mic capturing. To do this i call from the parent viewController the stop() function. But this give me this error – Aron Sep 19 '16 at 04:29

1 Answers1

1

I think it should be:

func stopMic(){
    if let _ = microphone {
        microphone.stopFetchingAudio()
    }
}

Explanation: The reason is you move from one view(where microphone is used) to another view without intializing it. And when you call the stop method from second view controller, it causes error, because microphone is NIL.

Bista
  • 7,869
  • 3
  • 27
  • 55
  • sorry was a failure in the post – Aron Sep 19 '16 at 04:27
  • Where have you initialized the microphone? – Bista Sep 19 '16 at 04:32
  • It is in a GameScene from SpriteKit defined. The idea is when i switch to a other view controller i will stop mic capturing. To do this i call from the parent viewController the stop() function. But this give me this error – Aron Sep 19 '16 at 04:32