录音
// MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? //播放器 var recorderSettingsDic:[NSObject : AnyObject]? //录音器设置参数数组 var volumeTimer:NSTimer!//定时器线程, 刷新音量 var aacPath:String? //录音存储路径 @IBOutlet var soundLodingImageView:UIImageView!//录音音量显示视图 //按下录音 @IBAction func downAction() { var error:NSError? //初始化录音器 recorder = AVAudioRecorder(URL: NSURL(string: aacPath!), settings: recorderSettingsDic, error: &error) if ((recorder) != nil) { //开启仪表计数功能 recorder!.meteringEnabled = true; //准备录音 recorder!.prepareToRecord() //开始录音 recorder!.record() //启动定时器 定时更新录音音量 volumeTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "levelTimer", userInfo: nil, repeats: true) } } //松开 结束录音 @IBAction func upAction() { //停止录音 recorder?.stop() //录制器释放 recorder = nil //暂停定时器 volumeTimer.invalidate() volumeTimer = nil //界面音量归0现实 soundLodingImageView.image = UIImage(named: "RecordingSignal001") } //播放录制的声音 @IBAction func playAction() { var playerError:NSError? //播放 player = nil; player = AVAudioPlayer(contentsOfURL: NSURL(string: aacPath!), error: &playerError) if (player == nil) { println("ERror creating player: \(playerError?.description)") }else{ player?.play() } } //定时器方法--检测录音音量 func levelTimer() { recorder!.updateMeters()//刷新音量数据 let averageV:Float = recorder!.averagePowerForChannel()//获取音量的平均值 let maxV:Float = recorder!.peakPowerForChannel()//获取音量的最大值 let lowPassResults:Double = pow(Double(), Double(0.05 * maxV)) println("\(lowPassResults)") if (lowPassResults>=0.8) { soundLodingImageView.image = UIImage(named: "RecordingSignal8.png") }else if(lowPassResults>=0.7){ soundLodingImageView.image = UIImage(named: "RecordingSignal7.png") }else if(lowPassResults>=0.6){ soundLodingImageView.image = UIImage(named: "RecordingSignal6.png") }else if(lowPassResults>=0.5){ soundLodingImageView.image = UIImage(named: "RecordingSignal5.png") }else if(lowPassResults>=0.4){ soundLodingImageView.image = UIImage(named: "RecordingSignal4.png") }else if(lowPassResults>=0.3){ soundLodingImageView.image = UIImage(named: "RecordingSignal3.png") }else if(lowPassResults>=0.2){ soundLodingImageView.image = UIImage(named: "RecordingSignal2.png") }else if(lowPassResults>=0.1){ soundLodingImageView.image = UIImage(named: "RecordingSignal1.png") }else{ soundLodingImageView.image = UIImage(named: "RecordingSignal1.png") } }
实现:
titleLabel.text = titleString let mp3Path = NSBundle.mainBundle().pathForResource("xiaoPingGuo", ofType: "mp3") let fileUrl = NSURL.fileURLWithPath(mp3Path!) audioPlayer = AVAudioPlayer(contentsOfURL: fileUrl, error: nil) audioPlayer.prepareToPlay() //以下录音使用 //初始化录音器 let session:AVAudioSession = AVAudioSession.sharedInstance() //初始化一个NSError对象,失败的时候可以获取失败原因 var error:NSError? //设置录音类型 session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error) //设置支持后台 session.setActive(true, error: &error) //获取Document目录 // let docDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String //2015年5月2后修改 let docDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, ] as! String //组合录音文件路径 aacPath = docDir + "/play.aac" //初始化字典 recorderSettingsDic = Dictionary() //字典添加设置参数 recorderSettingsDic!.updateValue(NSNumber(long: kAudioFormatMPEG4AAC), forKey: AVFormatIDKey) recorderSettingsDic!.updateValue(NSNumber(), forKey: AVSampleRateKey) recorderSettingsDic!.updateValue(NSNumber(), forKey: AVNumberOfChannelsKey) recorderSettingsDic!.updateValue(NSNumber(), forKey: AVLinearPCMBitDepthKey) recorderSettingsDic!.updateValue(NSNumber(bool: false), forKey: AVLinearPCMIsBigEndianKey) recorderSettingsDic!.updateValue(NSNumber(bool: false), forKey: AVLinearPCMIsFloatKey)