gpt4 book ai didi

ios - 用户说话时自动开始录音

转载 作者:行者123 更新时间:2023-11-28 07:48:14 27 4
gpt4 key购买 nike

我试图在用户开始说话时开始录音,并在用户说完后停止录音。我想限制最大记录音频长度。我无法在 AVAudioRecorderDelegate 中找到足够的功能。希望你能理解我的问题。提前致谢

@IBAction func recordAudio(_ sender: Any) {
recordingLabel.text = "Recording in progress..."
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) {
//Success
} else {
print("Could not save audio recording!")
}
}

最佳答案

录制音频当用户 tak1 你需要一些步骤

<强>1。用户允许您的所有应用使用麦克风

在您的 Info Plist 添加 Privacy - Microphone Usage Description in user Plist 并添加 Text Description

<强>2。保存录制文件的位置 用户 FileManager

<强>3。结束时间:使用audioRecorder.record(forDuration: 30)//记录30秒

检查完整代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var recordButton: UIButton!
var recordingSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!


override func viewDidLoad() {
super.viewDidLoad()

}

@IBAction func recordAudio(_ sender: Any) {
self.requestRecordPermission()
}

func requestRecordPermission() {
recordingSession = AVAudioSession.sharedInstance()
do {
try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { [unowned self] allowed in
DispatchQueue.main.async {
if allowed {
// User allow you to record

// Start recording and change UIbutton color
self.recordButton.backgroundColor = .red
self.startRecording()

} else {
// failed to record!
}
}
}
} catch {
// failed to record!
}
}
func startRecording() {

let audioFilename = getDocumentsDirectory().appendingPathComponent("recordedFile.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(forDuration: 30) // record for 30 Sec
recordButton.setTitle("Tap to Stop", for: .normal)
recordButton.backgroundColor = .green

} catch {
finishRecording(success: false)
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}

@objc func recordTapped() {
if audioRecorder == nil {
startRecording()
} else {
finishRecording(success: true)
}
}

public func finishRecording(success: Bool) {
audioRecorder.stop()
audioRecorder = nil

if success {
// record sucess
recordButton.backgroundColor = .green
} else {
// record fail

recordButton.backgroundColor = .yellow

}
}

}


extension ViewController :AVAudioRecorderDelegate{


func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if !flag {
finishRecording(success: false)
}
}
}

关于ios - 用户说话时自动开始录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50363164/

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