gpt4 book ai didi

ios - Swift:所需条件为假:文件!= nil

转载 作者:可可西里 更新时间:2023-11-01 00:37:37 27 4
gpt4 key购买 nike

应用程序运行正常,但只要我点击 chipmunkBut​​ton,应用程序就会崩溃并提供以下错误:

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()

}


}

最佳答案

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

var audioFile:AVAudioFile!

这意味着 audioFile 为零。好吧,在那之后你永远不会触摸 audioFile,所以它开始时为 nil 并保持为 nil。因此,当您到达这一行时,您会崩溃:

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

...正是因为 audioFile 为 nil。

关于ios - Swift:所需条件为假:文件!= nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com