一个录音Demo-Swift语言实现

96
lfb_CD
2015.10.16 23:55* 字数 260

源码地址:https://github.com/lfb-cd/recordDemo
文章地址:http://www.jianshu.com/p/f0b88355d7cb
文章日期:2015/10/16
我的微博:lfb_CD

github上找了一圈,居然没有一个Swift语言实现可以直接用的录音Demo,于是自己写一个吧!

分别定义AVAudioRecorderAVAudioPlayer的两个实例,一个用于录音,一个用于播放

var audioRecorder:AVAudioRecorder!
var audioPlayer:AVAudioPlayer!

//定义音频的编码参数,这部分比较重要,决定录制音频文件的格式、音质、容量大小等,建议采用AAC的编码方式

AVSampleRateKey//声音采样率
AVFormatIDKey //编码格式
AVNumberOfChannelsKey //采集音轨
AVEncoderAudioQualityKey //音频质量
let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),//声音采样率
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),//编码格式
AVNumberOfChannelsKey : NSNumber(int: 1),//采集音轨
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue))]//音频质量

//初始化实例,准备录音

        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!,
                settings: recordSettings)//初始化实例
            audioRecorder.prepareToRecord()//准备录音
        } catch {
        }

//定义并构建一个url来保存音频,音频文件名为ddMMyyyyHHmmss.caf

    func directoryURL() -> NSURL? {

        //根据时间设置存储文件名
        let currentDateTime = NSDate()
        let formatter = NSDateFormatter()
        formatter.dateFormat = "ddMMyyyyHHmmss"
        let recordingName = formatter.stringFromDate(currentDateTime)+".caf"
        print(recordingName)
        
        let fileManager = NSFileManager.defaultManager()
        let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
        let documentDirectory = urls[0] as NSURL
        let soundURL = documentDirectory.URLByAppendingPathComponent(recordingName)//将音频文件名称追加在可用路径上形成音频文件的保存路径
        return soundURL
    }

//开始录音

 @IBAction func startRecord(sender: AnyObject) {
        if !audioRecorder.recording {//判断是否正在录音状态
            let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setActive(true)
                audioRecorder.record()
                print("record!")
            } catch {
            }
        }
    }

//停止录音

        audioRecorder.stop()
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setActive(false)
            print("stop!!")
        } catch {
        }
    }

//开始播放

    @IBAction func startPlaying(sender: AnyObject) {  
        if (!audioRecorder.recording){
            do {
                try audioPlayer = AVAudioPlayer(contentsOfURL: audioRecorder.url)
                audioPlayer.play()
                print("play!!")
            } catch {
            }
        }
    }

//暂停播放

    @IBAction func pausePlaying(sender: AnyObject) {
        if (!audioRecorder.recording){
            do {
                try audioPlayer = AVAudioPlayer(contentsOfURL: audioRecorder.url)
                audioPlayer.pause()
                
                print("pause!!")
            } catch {
            }
        }  
    }

完整代码 下载地址请用力戳!:github 打赏也不必了,给俺一个star就够了!

改天会写几篇iOS蓝牙开发的文章,不日将和大家见面,敬请期待!

1445008077.jpeg

完!

iOS开发
Web note ad 1