我正在尝试制作一个简单的录音机。我正在使用Xcode-beta 7,并且我的代码基于这三个来源。
我正在使用以下代码:
var recordSettings = [
AVFormatIDKey: kAudioFormatAppleIMA4,
AVLinearPCMIsBigEndianKey: 0,
AVLinearPCMIsFloatKey: 0,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 32000
]
var session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
recorder = AVAudioRecorder(URL: filePath, settings: recordSettings, error: nil)
}catch{
print("Error")
}
但它说:“找不到类型为'AVAudioRecorder'的初始化程序,该初始化程序接受类型为'(URL:NSURL ?,设置:[String:AudioFormatID],错误:nil)'的参数列表”
我的输入不正是文档所要求的吗?
最佳答案
AVAudioRecorder不再需要error参数:
init(URL url: NSURL, settings settings: [String : AnyObject]) throws
另外,我需要解开filePath,如先前答案中所建议:
func recordSound(){
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let recordingName = "my_audio.wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0]
print(filePath)
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
audioRecorder = try AVAudioRecorder(URL: filePath!, settings: recordSettings as! [String : AnyObject])
} catch _ {
print("Error")
}
audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
关于ios - 使用AVAudioRecorder录制语音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31036892/