无法使用类型'()'的参数列表调用函数

时间:2015-08-05 08:08:45

标签: ios swift

我是IOS开发的新手。我正在关注的资源中AVAudioRecorder的构造函数是AVAudioRecorder(URL: filePath, settings: nil, error: nil),但我在Swift 2中编码,它有不同的构造函数。

错误无法调用带有'(')类型参数列表的函数发生在“try audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)”行

这是我的录音音频文件:

@IBAction func recordAudio(sender: UIButton) {
    stopButton.hidden = false
    recordButton.enabled = false
    recordingInProgress.hidden = false
    let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

    let currentDateTime = NSDate()
    let formatter = NSDateFormatter()
    formatter.dateFormat = "ddMMyyyy-HHmmss"
    let recordingName = formatter.stringFromDate(currentDateTime)+".wav"
    let pathArray = [dirPath, recordingName]
    let filePath = NSURL.fileURLWithPathComponents(pathArray)
    print(filePath)

    var session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord)

    } catch {}

    do {
        try audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)
    } catch {}

    audioRecorder.meteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.record()
}

2 个答案:

答案 0 :(得分:1)

在Swift 2中使用其初始化程序初始化AVAudioRecorder的方法是省略NSErrorPointer类型的参数并捕获它:

do 
{
    audioRecorder = try AVAudioRecorder(URL:filePath, settings:[:])
} 
catch let error as NSError
{
    printf(error.description)
}

答案 1 :(得分:0)

您缺少录音机初始化程序的错误部分:

audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)