AVAudioRecorder swift 2
Posted
技术标签:
【中文标题】AVAudioRecorder swift 2【英文标题】: 【发布时间】:2015-09-01 23:38:23 【问题描述】:我的AVAudioRecorder
工作正常,但自从升级到 swift 2 后,我似乎无法弄清楚如何正确配置它。我不断收到错误消息,提示无法调用 AVAudioRecorder
初始化程序,但我提供的参数对我来说看起来是正确的。
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
【问题讨论】:
recordingURL
与 directoryURL
?您在directoryURL()
中返回文件的名称,这可能会引起一些混乱。
你好,布罗瑟夫。这是记录器的有用代码 => github.com/genedelisa/AVFoundationRecorder
【参考方案1】:
directoryURL
是正确的,但它似乎被误认为是 recordingURL
。 recordSettings
也是连贯的。让我提供一个工作版本。
斯威夫特 3
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
► 在GitHub 上查找此解决方案,在Swift Recipes 上查找更多详细信息。
【讨论】:
我确实包含了大部分代码,但我跳过了它,因为它并没有真正参与正在显示的错误中。不过我会编辑它。 谢谢!我删除了内联错误参数并将其保留为audioRecorder = AVAudioRecorder(URL: recordingURL!, settings: recordSettings)
并将其放在 do/catch 中。那个错误描述真的不清楚。以上是关于AVAudioRecorder swift 2的主要内容,如果未能解决你的问题,请参考以下文章
Swift + AVAudioRecorder 非常安静地录制
Swift 运行时错误 (exc_bad_access) AVAudioRecorder, recordSettings
Swift AVAudioPlayer 不会播放用 AVAudioRecorder 录制的音频