[英]AVAudioRecorder Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
I learn an iOS course in Udacity. 我在Udacity学习了一门iOS课程。 I don't know why running this code as below.
我不知道为什么要运行以下代码。 It will show the message of Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) in the line of
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
它将在
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
行中显示线程1的消息:EXC_BAD_ACCESS(代码= 1,地址= 0x0) try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
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")
}
}
}
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()
}
You are using the wrong API. 您使用了错误的API。
URL(string:
is for strings representing a full URL including the scheme ( http://
, ftp://
, file://
). URL(string:
用于表示包含方案 ( http://
, ftp://
, file://
)的完整URL的字符串。
The correct API for file paths starting with a slash is URL(fileURLWithPath
. 以斜杠开头的文件路径的正确API是
URL(fileURLWithPath
。
Secondly NSSearchPathForDirectoriesInDomains
and concatenating path components manually is outdated. 其次,
NSSearchPathForDirectoriesInDomains
和手动连接路径组件已过时。 Use the URL related API, this solves the error by the way. 使用与URL相关的API,这可以解决错误。
let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documentsURL.appendingPathComponent("recordedVoice.wav")
This try!
这个
try!
cannot crash because the OS creates always the documents folder if it doesn't exist. 不会崩溃,因为OS总是创建documents文件夹(如果不存在)。
For API which can really throw
errors add error handling 对于确实会
throw
错误的API,请添加错误处理
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
try audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
...
} catch { print(error) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.