在Swift中录制音频

前端之家收集整理的这篇文章主要介绍了在Swift中录制音频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有谁知道我在哪里可以找到如何记录音频在Swift应用程序的信息?我一直在看一些音频播放示例,但我似乎无法找到任何实现音频录音。谢谢
这里是代码。您可以轻松地记录。在IBAction.Write这段代码将保存在文档中的记录通过名称recordTest.caf
//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()
    }


}

猜你在找的Swift相关文章