【问题标题】:Swift: required condition is false: file != nil斯威夫特:所需条件为假:文件!= nil
【发布时间】:2015-05-10 22:33:29
【问题描述】:

该应用程序运行良好,但只要我点击 ChimunkBut​​ton,应用程序就会崩溃并提供以下错误:

错误:AVAudioPlayerNode.mm:678:-[AVAudioPlayerNode scheduleFile:atTime:completionHandler:]:所需条件为假: 文件!= nil 2015-05-10 17:24:25.269 Pitch Perfect[50278:1897012] *** 由于未捕获的异常而终止应用程序 'com.apple.coreaudio.avfaudio',原因:'所需条件为假: 文件!= 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()

    }


}

【问题讨论】:

  • 所以,当你调用scheduleFile时,audioFile参数就是nil。你在哪里设置的?
  • 是的,您似乎没有在任何地方设置音频文件。

标签: ios swift null avaudioengine avaudioplayernode


【解决方案1】:

相当初级。只需阅读错误消息!你是说:

var audioFile:AVAudioFile!

这意味着audioFile 为零。好吧,在那之后你再也不会碰audioFile,所以它一开始是 nil 并且仍然是 nil。因此,当你到达这条线时,你会崩溃:

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

...正是因为audioFile 为零。

【讨论】:

    【解决方案2】:

    正如马特所说,audioFile 为 nil

    因此,要解决此问题,您必须进行设置。查看代码,您应该使用

        audioFile = AVAudioFile(forReading: receivedAudio.filePathUrl, error: nil)
    

    这将设置 audioFile 并且代码不会崩溃!

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 2017-07-22
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 2017-11-13
      相关资源
      最近更新 更多