你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS-錄音功能

IOS-錄音功能

編輯:IOS開發綜合

初始化AVAudioRecorder並設置錄音的參數和存儲地址:


[cpp]
//錄音設置  
    NSMutableDictionary *recordSetting = [[[NSMutableDictionary alloc]init] autorelease]; 
    //設置錄音格式  AVFormatIDKey==kAudioFormatLinearPCM  
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 
    //設置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質量)  
    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey]; 
    //錄音通道數  1 或 2  
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey]; 
    //線性采樣位數  8、16、24、32  
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
    //錄音的質量  
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey]; 
     
    NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/lll.aac", strUrl]]; 
    urlPlay = url; 
     
    NSError *error; 
    //初始化  
    recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error]; 
    //開啟音量檢測  
    recorder.meteringEnabled = YES; 
    recorder.delegate = self; 

//錄音設置
    NSMutableDictionary *recordSetting = [[[NSMutableDictionary alloc]init] autorelease];
    //設置錄音格式  AVFormatIDKey==kAudioFormatLinearPCM
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    //設置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質量)
    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
    //錄音通道數  1 或 2
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
    //線性采樣位數  8、16、24、32
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    //錄音的質量
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
   
    NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/lll.aac", strUrl]];
    urlPlay = url;
   
    NSError *error;
    //初始化
    recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];
    //開啟音量檢測
    recorder.meteringEnabled = YES;
    recorder.delegate = self;

 

識別音量的大小,從而顯示不同的圖片,來呈現出音量圖波動


[cpp]
[recorder updateMeters];//刷新音量數據  
    //獲取音量的平均值  [recorder averagePowerForChannel:0];  
    //音量的最大值  [recorder peakPowerForChannel:0];  
     
    double lowPassResults = pow(10, (0.05 * [recorder peakPowerForChannel:0])); 
    NSLog(@"%lf",lowPassResults); 
    //最大50  0  
    //圖片 小-》大  
    if (0<lowPassResults<=0.06) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_01.png"]]; 
    }else if (0.06<lowPassResults<=0.13) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_02.png"]]; 
    }else if (0.13<lowPassResults<=0.20) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_03.png"]]; 
    }else if (0.20<lowPassResults<=0.27) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_04.png"]]; 
    }else if (0.27<lowPassResults<=0.34) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_05.png"]]; 
    }else if (0.34<lowPassResults<=0.41) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_06.png"]]; 
    }else if (0.41<lowPassResults<=0.48) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_07.png"]]; 
    }else if (0.48<lowPassResults<=0.55) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_08.png"]]; 
    }else if (0.55<lowPassResults<=0.62) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_09.png"]]; 
    }else if (0.62<lowPassResults<=0.69) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_10.png"]]; 
    }else if (0.69<lowPassResults<=0.76) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_11.png"]]; 
    }else if (0.76<lowPassResults<=0.83) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_12.png"]]; 
    }else if (0.83<lowPassResults<=0.9) { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_13.png"]]; 
    }else { 
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_14.png"]]; 
    } 

[recorder updateMeters];//刷新音量數據
    //獲取音量的平均值  [recorder averagePowerForChannel:0];
    //音量的最大值  [recorder peakPowerForChannel:0];
   
    double lowPassResults = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
    NSLog(@"%lf",lowPassResults);
    //最大50  0
    //圖片 小-》大
    if (0<lowPassResults<=0.06) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_01.png"]];
    }else if (0.06<lowPassResults<=0.13) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_02.png"]];
    }else if (0.13<lowPassResults<=0.20) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_03.png"]];
    }else if (0.20<lowPassResults<=0.27) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_04.png"]];
    }else if (0.27<lowPassResults<=0.34) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_05.png"]];
    }else if (0.34<lowPassResults<=0.41) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_06.png"]];
    }else if (0.41<lowPassResults<=0.48) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_07.png"]];
    }else if (0.48<lowPassResults<=0.55) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_08.png"]];
    }else if (0.55<lowPassResults<=0.62) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_09.png"]];
    }else if (0.62<lowPassResults<=0.69) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_10.png"]];
    }else if (0.69<lowPassResults<=0.76) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_11.png"]];
    }else if (0.76<lowPassResults<=0.83) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_12.png"]];
    }else if (0.83<lowPassResults<=0.9) {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_13.png"]];
    }else {
        [self.imageView setImage:[UIImage imageNamed:@"record_animate_14.png"]];
    }

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved