有谁知道我在哪里可以找到如何记录音频在Swift应用程序的信息?我一直在看一些音频播放示例,但我似乎无法找到任何实现音频录音。谢谢
这里是代码。您可以轻松地记录。在IBAction.Write这段代码将保存在文档中的记录通过名称recordTest.caf
原文链接:https://www.f2er.com/swift/320855.html//declare instance variable var audioRecorder:AVAudioRecorder! func record(){ var audioSession:AVAudioSession = AVAudioSession.sharedInstance() audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,error: nil) audioSession.setActive(true,error: nil) var documents: AnyObject = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)[0] var str = documents.stringByAppendingPathComponent("recordTest.caf") var url = NSURL.fileURLWithPath(str as String) var recordSettings = [AVFormatIDKey:kAudioFormatAppleIMA4,AVSampleRateKey:44100.0,AVNumberOfChannelsKey:2,AVEncoderBitRateKey:12800,AVLinearPCMBitDepthKey:16,AVEncoderAudioQualityKey:AVAudioQuality.Max.rawValue ] println("url : \(url)") var error: NSError? audioRecorder = AVAudioRecorder(URL:url,settings: recordSettings,error: &error) if let e = error { println(e.localizedDescription) } else { audioRecorder.record() } }