本文介绍了AVAudioRecorder迅捷2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的AVAudioRecorder
工作正常,但是由于升级到swift 2,我似乎无法弄清楚如何正确配置它.我不断收到错误消息,说不能调用AVAudioRecorder
初始化程序,但是我提供的参数对我来说是正确的.
I had my AVAudioRecorder
working, but since upgrading to swift 2, I can't seem to figure out how to configure it correctly. I keep getting an error saying the AVAudioRecorder
initializer cannot be invoked, but the parameters I'm providing look correct to me.
var recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue))]
var recordingURL: NSURL? = nil
var audioRecorder:AVAudioRecorder!
func directoryURL() -> NSURL? {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let documentDirectory = urls[0] as NSURL
let soundURL = documentDirectory.URLByAppendingPathComponent("sound.m4a")
return soundURL
}
@IBAction func recordPressed(sender: AnyObject) {
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch _ {
}
do {
try audioSession.setActive(true)
} catch _ {
}
var error: NSError?
audioRecorder = AVAudioRecorder(URL: recordingURL, settings: recordSettings, error: &error)
if let e = error {
print(e.localizedDescription, terminator: "")
}
else
{
audioRecorder.record()
self.stopButton.enabled = true
self.playButton.enabled = false
self.recordButton.enabled = false
}
}
推荐答案
directoryURL
是正确的,但似乎误认为recordingURL
. recordSettings
也是连贯的.我提供一个工作版本.
directoryURL
is proper, yet it appears mistaken for recordingURL
. The recordSettings
are coherent as well. Let me offer a working version.
var audioRecorder:AVAudioRecorder!
let recordSettings = [
AVSampleRateKey : NSNumber(value: Float(44100.0)),
AVFormatIDKey : NSNumber(value:Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(value: Int32(1)),
AVEncoderAudioQualityKey :
NSNumber(value: Int32(AVAudioQuality.medium.rawValue))]
override func viewDidLoad() {
super.viewDidLoad()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioRecorder = AVAudioRecorder(url: directoryURL()!,
settings: recordSettings)
audioRecorder.prepareToRecord()
} catch {}
}
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("sound.m4a")
return soundURL
}
@IBAction func doRecordAction(_ sender: AnyObject) {
if !audioRecorder.isRecording {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true)
audioRecorder.record()
} catch {}
}
}
@IBAction func doStopAction(_ sender: AnyObject) {
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(false)
} catch {}
}
旧版:Swift 2
var audioRecorder:AVAudioRecorder!
let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey :
NSNumber(int: Int32(AVAudioQuality.Medium.rawValue))]
override func viewDidLoad() {
super.viewDidLoad()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!,
settings: recordSettings)
audioRecorder.prepareToRecord()
} catch {}
}
func directoryURL() -> NSURL? {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory,
inDomains: .UserDomainMask)
let documentDirectory = urls[0] as NSURL
let soundURL = documentDirectory.URLByAppendingPathComponent("sound.m4a")
return soundURL
}
@IBAction func doRecordAction(sender: AnyObject) {
if !audioRecorder.recording {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true)
audioRecorder.record()
} catch {}
}
}
@IBAction func doStopAction(sender: AnyObject) {
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(false)
} catch {}
}
► Find this solution on GitHub and additional details on Swift Recipes.
这篇关于AVAudioRecorder迅捷2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!