iosswiftoption-typeezaudio

EZAudio stopfetching audio


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.


Solution

  • 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.