Search code examples
iosswiftnullavaudioengineavaudioplayernode

Swift: required condition is false: file != nil


The app runs fine but as soon as I tap chipmunkButton the app crashes and provides the following error:

ERROR: AVAudioPlayerNode.mm:678: -[AVAudioPlayerNode scheduleFile:atTime:completionHandler:]: required condition is false: file != nil 2015-05-10 17:24:25.269 Pitch Perfect[50278:1897012] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: file != nil'

import UIKit
import AVFoundation

class PlaySoundsViewController: UIViewController {

    var audioRecording = AVAudioPlayer()
    var receivedAudio:RecordedAudio!
    var audioEngine:AVAudioEngine!
    var audioFile:AVAudioFile!



    func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer  {


        //1
        var path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
        var url = NSURL.fileURLWithPath(path!)

        //2
        var error: NSError?

        //3
        var audioPlayer:AVAudioPlayer?
        audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &error)

        //4
        return audioPlayer!
    }


    override func viewDidLoad() {
        super.viewDidLoad()


        audioRecording = AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl, error: nil)
        audioRecording.enableRate = true

        audioEngine = AVAudioEngine()

            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func slowButton(sender: UIButton) {

        playButtons()
        audioRecording.rate = 0.5

    }


    @IBAction func fastSound(sender: UIButton) {
        audioRecording.rate = 2.0
        playButtons()
    }


    @IBAction func chipmunkButton(sender: UIButton) {

        playAudioWithVariablePitch(1000)

    }

    func playAudioWithVariablePitch(pitch: Float) {
        audioRecording.stop()
        audioEngine.stop()
        audioEngine.reset()

        var audioPlayerNode = AVAudioPlayerNode()
        audioEngine.attachNode(audioPlayerNode)

        var changePitchEffect = AVAudioUnitTimePitch()
        changePitchEffect.pitch = pitch
        audioEngine.attachNode(changePitchEffect)

        audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
        audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)

        audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
        audioEngine.startAndReturnError(nil)

        audioPlayerNode.play()


    }


    @IBAction func stopButton(sender: UIButton) {
       audioRecording.stop()

    }

    func playButtons() {
        audioRecording.stop()
        audioRecording.currentTime = 0
        audioRecording.play()

    }


}

Solution

  • Pretty elementary. Just read the error message! You are saying:

    var audioFile:AVAudioFile!
    

    That means audioFile is nil. Well, you never touch audioFile after that, so it starts out as nil and remains nil. Thus, when you get to this line, you crash:

    audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
    

    ... exactly because audioFile is nil.