如何用Swift录音

tinyfool 发布于 2016年01月10日
无人欣赏。
class Recorder {

    var audioRecorder:AVAudioRecorder!

    func record(){

        let audioSession:AVAudioSession = AVAudioSession.sharedInstance()

        try! audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)

        try! audioSession.setActive(true)

        let documents: AnyObject = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory,  NSSearchPathDomainMask.UserDomainMask, true)[0]

        let str =  documents.stringByAppendingPathComponent("recordTest.caf")
        let url = NSURL.fileURLWithPath(str as String)

        let recordSettings:[String : AnyObject] = [AVFormatIDKey:NSNumber(unsignedInt:kAudioFormatAppleIMA4),
            AVSampleRateKey:44100.0,
            AVNumberOfChannelsKey:2,AVEncoderBitRateKey:12800,
            AVLinearPCMBitDepthKey:16,
            AVEncoderAudioQualityKey:AVAudioQuality.Max.rawValue]

        print("url : (url)")

        try! audioRecorder = AVAudioRecorder(URL:url, settings: recordSettings)
        audioRecorder.record()
    }
}
共1条回复
chenyf 回复于 2016年01月10日

pretty cool!

登录 或者 注册