gpt4 book ai didi

ios - Swift 和录音

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:16 33 4
gpt4 key购买 nike

我正在快速构建一个 iOS 应用程序,客户端返回一个录音问题,基本上它不记录任何内容。他在装有 ios 9.x 的 iPhone 5/5s 上进行了测试。检查权限以及他的手机是否有足够的空间,这些都不是问题。

就个人而言,在模拟器上使用装有 ios 10.x 和 iphone 5 的 iphone 6s 设备进行测试,记录正常。任何人之前都遇到过这个,或者我做错了一些我无法理解的事情。

为说明应用程序功能的不同功能添加了注释。

protocol RecordAndPlayManagerDelegate : class{
func recorderDidStopRecording()
}

class RecordAndPlayManager: NSObject, AVAudioRecorderDelegate {
var soundRecorder: AVAudioRecorder!
let recordSettings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
class func directoryURL() -> URL? {
let fileManager = FileManager.default
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = urls[0] as URL
let soundURL = documentDirectory.appendingPathComponent("recording.m4a")
return soundURL
}

//calling this from outside to get the audio
class func getLocalOrRemoteRecording(_ recordingID : String!) -> URL?{

let fileManager = FileManager.default
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = urls[0] as URL
let soundURL = documentDirectory.appendingPathComponent(recordingID)

if (fileManager.fileExists(atPath: soundURL.path)){
return soundURL
}

let path = kBaseServerURLNOPORT + "data/" + recordingID
let url = URL(string: path)

return url

}

//called from outside, recordingID -> name of the file
class func storeRecording(_ recordingID : String!) -> URL? {
let fileManager = FileManager.default
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = urls[0] as URL
let soundURL = documentDirectory.appendingPathComponent("recording.m4a")

let data = try? Data(contentsOf: soundURL)
//data here has only 28bytes and from this I know it didn't record. In my tests, I always get at least 20000 bytes.

let newSoundURL = documentDirectory.appendingPathComponent(recordingID)
try? data?.write(to: newSoundURL, options: [.atomic])

do {
try FileManager.default.removeItem(at: soundURL)
} catch _ {
print("failed to delete file")
return newSoundURL
}

return newSoundURL
}

//called on viewDidLoad in another screen
func setupRecorder() {
let url = DRMessageRecordAndPlayManager.directoryURL()!
do {
try self.soundRecorder = AVAudioRecorder(url: url, settings: self.recordSettings)
} catch _ {
return
}
soundRecorder.delegate = self
soundRecorder.prepareToRecord()
}

//calling this from outside to start recording
func startRecording() {
if !self.soundRecorder.isRecording {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setActive(true)
soundRecorder.record(forDuration: 15)
} catch {
}
}
}

//called from outside when I hit stop
func stopRecording() {

self.soundRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()

do {
try audioSession.setActive(false)
} catch {
}
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
self.delegate?.recorderDidStopRecording()
}
}

最佳答案

我的猜测是 startRecording() 中的 catch 正在按预期工作并防止崩溃,但您没有记录它。

我建议像这样更改两个 catch 语句打印提供的错误对象:

catch {
// or use an alert controller to display the error
print("caught error: \(error)")
}

关于ios - Swift 和录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41264244/

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