gpt4 book ai didi

ios - AVAudioRecorder 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x0)

转载 作者:行者123 更新时间:2023-11-28 20:56:06 25 4
gpt4 key购买 nike

我在优达学城学习 iOS 类(class)。我不知道为什么要运行下面的代码。它将在 try! 行中显示 Thread 1 的消息:EXC_BAD_ACCESS (code=1, address=0x0) 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()
}

关于ios - AVAudioRecorder 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52277971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com