AVAudioRecorder 录音



//导入系统库 AVFoundation

    


    var _recorder :AVAudioRecorder?

    var _player: AVAudioPlayer?

    override func viewDidLoad() {

        super.viewDidLoad()

        //设置录制文件的访问路径

        let cachePathArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)

        let cachePath = cachePathArray[0] as! String

        let realPath = cachePath.stringByAppendingPathComponent("recorder.wav")

        print("路径:\(realPath)")

        let url = NSURL(fileURLWithPath: realPath)

        

        //设置录音参数

        let dict: Dictionary<String,AnyObject> = [

                        AVEncoderAudioQualityKey :  AVAudioQuality.Min.rawValue,  //录音质量

                        AVEncoderBitRateKey : 2 , //比特率

                        AVSampleRateKey : 44100] //采样率

        var error: NSError? = nil

        _recorder = AVAudioRecorder(URL: url, settings: dict, error: &error)

        _recorder?.delegate = self

        if error == nil {

            //出错

        }else {

            _recorder?.prepareToRecord() //准备去录音

        }

        //初始化播放器

        _player = AVAudioPlayer(contentsOfURL: url, error: nil)

        _player?.delegate = self

    }

    

//MARK: recorderDelegate 中断代理

    func audioRecorderBeginInterruption(recorder: AVAudioRecorder!) {

        _recorder?.pause()

    }

    func audioRecorderEndInterruption(recorder: AVAudioRecorder!) {

        _recorder?.prepareToRecord()

        _recorder?.record()

    }

//MARK: playerDelegate 中断代理

    func audioPlayerBeginInterruption(player: AVAudioPlayer!) {

        _player?.pause()


    }

    func audioPlayerEndInterruption(player: AVAudioPlayer!) {

        _player?.prepareToPlay()

        _player?.play()

    }

  Demo下载:  ZIP 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值