我是优达学城 iOS 开发人员的学生,正在开发用于录制和播放音频的应用程序 PitchPerfect。我正在使用 Xcode 10.2.1 以及模拟器和 Swift 4。我一直在密切关注类(class)说明,但我的 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 类(class)中获得它时,但是我的构建失败了并且出现了错误:
“AVAudioRecorder?”类型的值?没有成员“URL”
最佳答案
您的代码似乎已过时,url
已小写,并使用传递的recorder
参数
performSegue(withIdentifier: "stopRecording", sender: recorder.url)
阅读 documentation 总是值得的
而且这四行根本行不通(URL(string
is inappropriate)而且这是一种连接路径的可怕方式
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: [:])
旁注:不鼓励尝试!
,除非代码不会崩溃(比如在 FileManager
行中)
关于swift - 如何修复 Xcode 错误 : Value of type 'AVAudioRecorder?' has no member 'URL' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56547763/