ios开发——实用技术篇Swift篇&录音

录音

 

  1     // MARK: - 录音
  2     /*----- 录音 ------*/
  3     
  4     var recorder:AVAudioRecorder?   //录音器
  5     var player:AVAudioPlayer?   //播放器
  6     var recorderSettingsDic:[NSObject : AnyObject]?   //录音器设置参数数组
  7     var volumeTimer:NSTimer!//定时器线程, 刷新音量
  8     var aacPath:String? //录音存储路径
  9     
 10      @IBOutlet var soundLodingImageView:UIImageView!//录音音量显示视图
 11     
 12     
 13     //按下录音
 14     @IBAction func downAction()
 15     {
 16         var error:NSError?
 17         
 18         //初始化录音器
 19         recorder = AVAudioRecorder(URL: NSURL(string: aacPath!), settings: recorderSettingsDic, error:  &error)
 20         
 21         
 22         if ((recorder) != nil) {
 23             
 24             //开启仪表计数功能
 25             recorder!.meteringEnabled = true;
 26             
 27             //准备录音
 28             recorder!.prepareToRecord()
 29             
 30             //开始录音
 31             recorder!.record()
 32             
 33             //启动定时器 定时更新录音音量
 34             volumeTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "levelTimer", userInfo: nil, repeats: true)
 35         }
 36         
 37     }
 38 
 39     
 40      //松开 结束录音
 41     @IBAction func upAction()
 42     {
 43         //停止录音
 44         recorder?.stop()
 45         
 46         //录制器释放
 47         recorder = nil
 48         
 49         //暂停定时器
 50         volumeTimer.invalidate()
 51         volumeTimer = nil
 52         
 53         //界面音量归0现实
 54         soundLodingImageView.image = UIImage(named: "RecordingSignal001")
 55     }
 56     
 57     //播放录制的声音
 58     @IBAction func playAction()
 59     {
 60         var playerError:NSError?
 61         
 62         //播放
 63         player = nil;
 64         player = AVAudioPlayer(contentsOfURL: NSURL(string: aacPath!), error: &playerError)
 65         
 66         if (player == nil)
 67         {
 68             println("ERror creating player: \(playerError?.description)")
 69         }else{
 70             player?.play()
 71         }
 72     }
 73     
 74     
 75     //定时器方法--检测录音音量
 76     func levelTimer()
 77     {
 78         recorder!.updateMeters()//刷新音量数据
 79         
 80         let averageV:Float = recorder!.averagePowerForChannel(0)//获取音量的平均值
 81         
 82         let maxV:Float = recorder!.peakPowerForChannel(0)//获取音量的最大值
 83         
 84         let lowPassResults:Double = pow(Double(10), Double(0.05 * maxV))
 85         println("\(lowPassResults)")
 86         
 87         
 88         if (lowPassResults>=0.8) {
 89             soundLodingImageView.image = UIImage(named: "RecordingSignal8.png")
 90         }else if(lowPassResults>=0.7){
 91             soundLodingImageView.image = UIImage(named: "RecordingSignal7.png")
 92         }else if(lowPassResults>=0.6){
 93             soundLodingImageView.image = UIImage(named: "RecordingSignal6.png")
 94         }else if(lowPassResults>=0.5){
 95             soundLodingImageView.image = UIImage(named: "RecordingSignal5.png")
 96         }else if(lowPassResults>=0.4){
 97             soundLodingImageView.image = UIImage(named: "RecordingSignal4.png")
 98         }else if(lowPassResults>=0.3){
 99             soundLodingImageView.image = UIImage(named: "RecordingSignal3.png")
100         }else if(lowPassResults>=0.2){
101             soundLodingImageView.image = UIImage(named: "RecordingSignal2.png")
102         }else if(lowPassResults>=0.1){
103             soundLodingImageView.image = UIImage(named: "RecordingSignal1.png")
104         }else{
105             soundLodingImageView.image = UIImage(named: "RecordingSignal1.png")
106         }
107         
108     }

 实现:

 1         titleLabel.text = titleString
 2         
 3         let mp3Path = NSBundle.mainBundle().pathForResource("xiaoPingGuo", ofType: "mp3")
 4         let fileUrl = NSURL.fileURLWithPath(mp3Path!)
 5         
 6         audioPlayer = AVAudioPlayer(contentsOfURL: fileUrl, error: nil)
 7         audioPlayer.prepareToPlay()
 8         
 9         
10         //以下录音使用
11         
12         //初始化录音器
13         let session:AVAudioSession = AVAudioSession.sharedInstance()
14         
15         //初始化一个NSError对象,失败的时候可以获取失败原因
16         var error:NSError?
17         
18         //设置录音类型
19         session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error)
20         
21         //设置支持后台
22         session.setActive(true, error: &error)
23         
24         //获取Document目录
25 //        let docDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
26         
27         //2015年5月2后修改
28         let docDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
29         
30         //组合录音文件路径
31         aacPath = docDir + "/play.aac"
32         
33         //初始化字典
34         recorderSettingsDic = Dictionary()
35         
36         //字典添加设置参数
37         recorderSettingsDic!.updateValue(NSNumber(long: kAudioFormatMPEG4AAC), forKey: AVFormatIDKey)
38         recorderSettingsDic!.updateValue(NSNumber(long: 1000), forKey: AVSampleRateKey)
39         recorderSettingsDic!.updateValue(NSNumber(long: 2), forKey: AVNumberOfChannelsKey)
40         recorderSettingsDic!.updateValue(NSNumber(long: 8), forKey: AVLinearPCMBitDepthKey)
41         recorderSettingsDic!.updateValue(NSNumber(bool: false), forKey: AVLinearPCMIsBigEndianKey)
42         recorderSettingsDic!.updateValue(NSNumber(bool: false), forKey: AVLinearPCMIsFloatKey)

 

 
 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。