在 Swift 中录制音频 [英] Recording audio in Swift

查看:24
本文介绍了在 Swift 中录制音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我在哪里可以找到有关如何在 Swift 应用程序中录制音频的信息?我一直在查看一些音频播放示例,但似乎无法找到有关实现音频录制的任何内容.谢谢

Does anyone know where I can find info on how to record audio in a Swift application? I've been looking at some of the audio playback examples but I can't seem to be able to find anything on implementing the audio recording. Thanks

推荐答案

这里是代码.你可以很容易地记录.把这段代码写在IBAction上.它会将录音保存在Documents 按名称 recordTest.caf

Here is code.You can record easily.Write this code on IBAction.It will save the recording in Documents by name 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 中录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆