我在Udacity学习iOS课程。我不知道为什么要运行以下代码。它将在try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
import UIKit
import AVFoundation
class RecordViewController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder = AVAudioRecorder()
@IBOutlet weak var recordingLabel: UITextField!
@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, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
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 wasn't successful")
}
}
}
答案 0 :(得分:1)
i think you must use try like this AVAudioPlayer
var resourcePath = url //your url
var objectData = Data(contentsOf: NSURL(string: resourcePath)!)
var error: Error!
do {
audioPlayer = try AVAudioPlayer(objectData)
}
catch let error {
}
audioPlayer.numberOfLoops = 0
audioPlayer.volume = 1.0
audioPlayer.prepareToPlay()
if audioPlayer == nil {
print("\(error.description)")
}
else {
audioPlayer.play()
}
答案 1 :(得分:1)
您使用了错误的API。
URL(string:
用于表示包含完整URL的字符串,包括方案(http://
,ftp://
,file://
)。
以斜杠开头的文件路径的正确API是URL(fileURLWithPath
。
第二NSSearchPathForDirectoriesInDomains
和手动连接路径组件已过时。使用与网址相关的API,这样可以解决错误。
let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documentsURL.appendingPathComponent("recordedVoice.wav")
此try!
不会崩溃,因为如果不存在,操作系统将始终创建documents文件夹。
对于确实可以throw
错误添加错误处理的API
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
try audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
...
} catch { print(error) }