2011-11-25 85 views
4

我想记录用户背后播放背景音乐的用户声音。我可以使用AVAudioSessionCategoryPlayAndRecord同时设置会话并播放背景和记录。但它的录音很多的噪音,在iPhone应用程序中录制用户语音时减少噪音?

有没有人有一个想法如何减少噪音?

+3

我希望你不介意,但我改变了你的标题从保存噪音到降低噪音,这样做更有意义。 – sosborn

+0

得到了解决你的问题? –

+0

是的,我得到了解决方案 – Aadil

回答

1
#define DOCUMENTS [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
#define PATH_ARQUIVO [DOCUMENTS stringByAppendingPathComponent:@"gravacao.ma4"] 



-(IBAction) recordAudio:(UIButton *)sender { 

     NSURL* urlArquivo = [[NSURL alloc] initFileURLWithPath:PATH_ARQUIVO]; 

     NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys: 
     [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, 
     [NSNumber numberWithInt:16], AVEncoderBitRateKey, 
     [NSNumber numberWithInt:2], AVNumberOfChannelsKey, 
     [NSNumber numberWithFloat:44.1], AVSampleRateKey, 
            nil]; 
     NSError* error; 

     self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:urlArquivo settings:dic error:&error]; 


     if (error) { 
      NSLog(@"error: %@", [erro localizedDescription]); 
     } else { 
      //buffering 
      [self.audioRecorder prepareToRecord]; 
      //recording 
      [self.audioRecorder record]; 
     } 
    } 

    -(IBAction) stopRecorder:(UIButton *)sender { 
     if ([self.audioRecorder isRecording]) { 
      [self.audioRecorder stop]; 
     } 
    } 



    -(IBAction) PlayAudio:(UIButton *)sender { 
     NSURL* urlArquivo = [[NSURL alloc] initFileURLWithPath:PATH_ARQUIVO]; 

     NSError* error; 
     self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlArquivo error:&error]; 

     if (erro) { 
      NSLog(@"error %@", [error localizedDescription]); 
     } else { 
      self.audioPlayer.numberOfLoops = HUGE_VALF; 
      self.audioPlayer.enableRate = YES; 
      [self.audioPlayer prepareToPlay]; 
      [self.audioPlayer play]; 
     } 
    } 

    -(IBAction) stopPlaying:(UIButton *)sender { 
     if ([self.audioPlayer isPlaying]) { 
      [self.audioPlayer stop]; 
     } 
    } 

    -(IBAction) changeRate:(UISlider *)sender { 
     self.audioPlayer.rate = sender.value * 2; 
     /* it's a UISlider, max value = 1, min = 0 */ 
    }