如何修复Xcode错误:类型为“ AVAudioRecorder”的值?没有成员“ URL”

时间:2019-06-11 16:06:08

标签: swift xcode

我是Udacity iOS开发人员的学生,正在研究PitchPerfect应用程序,该应用程序可以记录和播放音频。我将Xcode 10.2.1与模拟器和Swift 4一起使用。我一直在密切关注类说明,但是RecordSoundsViewController.swift中的代码出现错误。我的构建不断失败。我收到以下错误:

“ AVAudioRecorder”类型的值?没有成员“ URL”

我希望您能提出建议,以继续前进并编译我的构建。

我尝试将Xcode版本更新(正在运行的当前版本10.2.1)并切换到Swift 4.2,但这似乎无济于事。

//
//  RecordSoundsViewController.swift
//  PitchPerfect
//
//  Created by Shara Karasic on 6/1/19.
//  Copyright © 2019 Shara Karasic. All rights reserved.
//

import UIKit
import AVFoundation

class RecordSoundsViewController: UIViewController,       AVAudioRecorderDelegate {

    var audioRecorder: AVAudioRecorder!
    var recordedAudioURL:URL!

    @IBOutlet weak var recordingLabel: UILabel!
    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var stopRecordingButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        stopRecordingButton.isEnabled = false
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print ("viewWillAppear called")
    }

    @IBAction func recordAudio(_ sender: AnyObject) {
        recordingLabel.text = "Recording in progress"
        stopRecordingButton.isEnabled = true
        recordButton.isEnabled = false

        let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
        let recordingName = "recordedVoice.wav"
        let pathArray = [dirPath, recordingName]
        let filePath = URL(string: pathArray.joined(separator: "/"))

        let session = AVAudioSession.sharedInstance()
        try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeDefault, options: AVAudioSession.CategoryOptions.defaultToSpeaker)

        try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
        audioRecorder.isMeteringEnabled = true
        audioRecorder.prepareToRecord()
        audioRecorder.record()
    }
    @IBAction func stopRecording(_ sender: Any) {
        recordButton.isEnabled = true
        stopRecordingButton.isEnabled = false
        recordingLabel.text = "Tap to Record"
        audioRecorder.stop()
        let audioSession = AVAudioSession.sharedInstance()
        try! audioSession.setActive(false)
    }
    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        if flag {
            performSegue(withIdentifier: "stopRecording", sender: audioRecorder.URL)
        } else {
            print ("recording was not successful")
        }
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "stopRecording" {
            let playSoundsVC = segue.destination as! PlaySoundsViewController
            let recordedAudioURL  = audioRecorder.url
            playSoundsVC.recordedAudioURL = recordedAudioURL
        }
    }
}

我希望这段代码能够平稳运行,尤其是当我直接从Udacity课程获得代码时,但是我的构建失败并且出现了错误:

“ AVAudioRecorder”类型的值?没有成员“ URL”

1 个答案:

答案 0 :(得分:1)

您的代码似乎已过时,url小写,并使用传递的recorder参数

performSegue(withIdentifier: "stopRecording", sender: recorder.url)

阅读documentation

总是值得的

这四行根本不起作用(URL(string是不合适的),这是连接路径的可怕方法

let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
let recordingName = "recordedVoice.wav"
let pathArray = [dirPath, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))

替换为

let dirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = dirURL.appendingPathComponent("recordedVoice.wav")

...

try! audioRecorder = AVAudioRecorder(url: fileURL, settings: [:])

旁注:不建议使用try!,除非代码无法崩溃(例如在FileManager行中)