ios - Swift 4 - 覆盖 AVAudioRecorder 录音
问题描述
我正在使用 AvAudioRecorder 在我的 iOS 应用程序中进行录音。我可以进行初始录制,但我想给用户重新录制的选项。
我有这些定义:
var recordingSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!
在我的 viewDidLoad 我有这个:
recordingSession = AVAudioSession.sharedInstance()
do {
try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { result in
DispatchQueue.main.async {
if(result == true)
{
}
else
{
}
}
}
}
catch
{
}
然后我有我的录制按钮:
@IBAction func recordButtonPressed(_ sender: Any) {
if audioRecorder == nil {
startRecording()
} else {
finishRecording(success: true)
}
}
这是我开始录制和完成录制的方法:
func startRecording() {
audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a")
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
recordButton.setTitle("Tap to Stop", for: .normal)
} catch {
finishRecording(success: false)
}
}
func finishRecording(success: Bool) {
audioRecorder.stop()
audioRecorder = nil
if success {
recordButton.setTitle("Tap to Re-record", for: .normal)
audioSlider.isHidden = false
playButton.isHidden = false
uploadButton.isHidden = false
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
self.audioPlayer = try AVAudioPlayer(contentsOf: audioFilename)
self.audioPlayer?.prepareToPlay()
}catch{
}
} else {
recordButton.setTitle("Tap to Record", for: .normal)
}
}
问题是我尝试重新录制的任何内容都没有。录制完成后,我尝试将 audioRecorder 设置为 nil,但这根本没有帮助。
解决方案
推荐阅读
- typescript - 从父类访问继承类的成员
- python - 无法在 VSCode 中运行长 python 文本
- javascript - 如何修复 TinyMCE 的样式?
- excel - 如何根据过滤后的 TABLE 创建数据验证列表?
- java - 将 Spring Boot Jpa 中的三个实体相互连接起来的问题
- r - bookdown 问题 - knitr::include_graphics() - “无法加载图片或 PDF 文件”使用图像 URL
- python - Python中的ATM脚本
- oauth-2.0 - Azure AD ADAL 隐式流说明
- c# - 仅针对有保证的作业过滤“查找所有参考”的快捷方式?
- android - 如何获取 Buildozer 的最新 SDK 版本