AVAudioRecorder进行录音
程序员文章站
2024-02-20 15:37:46
...
import UIKit
import AVFoundation
/**
AVAudioRecorder也需要强引用防止其被意外释放
并且需要告诉用户使用NSMicrophoneUsageDescription权限,否则会导致崩溃
*/
class ViewController: UIViewController {
var audioRecorder:AVAudioRecorder!
override func viewDidLoad() {
super.viewDidLoad()
let destinationURL = URL.init(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!)
do {
try audioRecorder = AVAudioRecorder(url: destinationURL, settings: [:])
} catch {
print(error)
}
audioRecorder.prepareToRecord()
//开始录音
audioRecorder.record()
/**
//暂停录音
audioRecorder.pause()
//结束录音
audioRecorder.stop()
*/
}
}
上一篇: Swift - Associated Object
下一篇: Swift 3.0 基础部分