简体   繁体   English

AVAudioRecorder线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)

[英]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.

相关问题 下载任务中的AFNetworking EXC_BAD_ACCESS(代码= 2,地址= 0x0) - AFNetworking EXC_BAD_ACCESS (code=2, address=0x0) in downloadTask EXC_BAD_ACCESS代码= 2地址= 0x0 - EXC_BAD_ACCESS code=2 address=0x0 收到EXC_BAD_ACCESS(代码= 2,地址= 0x0)错误 - getting a EXC_BAD_ACCESS (code=2, address=0x0) error EXC_BAD_ACCESS(代码= 1,地址= 0x0) - EXC_BAD_ACCESS (code=1, address=0x0) 线程1:生成EXC_BAD_ACCESS(代码= 1,地址= 0x0)问题 - Thread 1 : EXC_BAD_ACCESS (Code = 1, address = 0x0) issue generated Swift项目与线程1崩溃:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - Swift project crashing with Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x0) 线程1:Xcode 6 beta 7中的println()上的EXC_BAD_ACCESS(code = 1,address = 0x0) - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) on println() in Xcode 6 beta 7 线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)以迅捷方式工作时尝试登录以进行Spotify - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) When working in swift trying to login to spotify 场景视点线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - Scene Point of View Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) iOS 应用程序使用自定义 Tensorflow tflite 模型抛出错误 Thread 4: EXC_BAD_ACCESS (code=1, address=0x0) - iOS app throws error Thread 4: EXC_BAD_ACCESS (code=1, address=0x0) with custom Tensorflow tflite model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM