Reduce Noise While Recording users Voice in iphone Application? - iphone

I am trying to record users voice with background music playing behind. I am able to set a session and play background and record concurrently using AVAudioSessionCategoryPlayAndRecord. But it's recording lot's of noice,
Does any one have an idea how to reduce the noise?

#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 */
}

Related

Re-recording with AVAudioRecorder

I can record with the setup below - it works first time, but then when I try again the file is always 8192 bytes, i.e. not a correct recording.
-(void) startRecording
{
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 11025.0f], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityLow], AVEncoderAudioQualityKey,
nil];
NSString *filenameBasedOnTime = [[NSDate date] description];
if (_recordedFileURL) _recordedFileURL = nil;
_recordedFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:filenameBasedOnTime]];
NSError* error;
if (_audioRecorder) _audioRecorder = nil;
_audioRecorder = [[AVAudioRecorder alloc] initWithURL:_recordedFileURL settings:settings error:&error];
_audioRecorder.delegate = self;
if (error)
{
return;
}
[_audioRecorder prepareToRecord];
_audioRecorder.meteringEnabled = YES;
[_audioRecorder record];
}
-(void) stopRecord
{
[_audioRecorder stop];
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
[self saveRecording];
}
-(void) saveRecording
{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_recordedFileURL.relativeString]];
NSLog(#"Recording data size = %i", [data length]);
}
It is called inside a UIPopoverController if that helps...
Since found out that the problem was I was missing
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
From here iPhone SDK: AVAudioRecorder will not record after calling [AVPlayer play]

Trying to record and play audio at the same time, but it's quiet

Okay so I've tried to look this up so it isn't a duplicate but I might have missed something. Anyways, in my app I have a song that should start playing when I hit record. So when I hit record, the AVAudioRecorder starts recording and my already initialized AVAudioPlayer starts playing the song. Yet the song's volume becomes very quiet. I know it isn't the song because if I simply play the song without attempting to record at the same time, it plays at full volume. Any help guys? Thanks.
How I'm Initializing:
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44010.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(#"error: %#", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:"Rang De"
ofType:#"mp3"]];
audioPlayerForPreloadedMusic = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else
{
audioPlayerForPreloadedMusic.delegate = self;
[audioPlayerForPreloadedMusic prepareToPlay];
}
How I'm Playing:
-(void) recordAudio
{
if (!audioRecorder.recording)
{
playButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
if(!audioPlayerForPreloadedMusic.playing)
[audioPlayerForPreloadedMusic play];
else {
NSLog(#"music is playing, so won't play");
}
}
}
You are playing the song out the earpiece when you start recording, instead of out of the speaker.
There is an audio session override to prevent this automatic switch. See using: kAudioSessionProperty_OverrideAudioRoute and kAudioSessionOverrideAudioRoute_Speaker in Apple's iOS API documentation.

Saving audio files and navigating between views

I'm having trouble saving recorded audio files and playing them back after navigating between views.
I've been using the tutorial at http://www.techotopia.com/index.php/Recording_Audio_on_an_iPhone_with_AVAudioRecorder_%28iOS_4%29
I've added in a line to save the audio file, so my prepareForAudioRecording looks like the code below. This method is called in viewDidLoad.
-(void) prepareForAudioRecording
{
btnPlay.enabled = NO;
btnStop.enabled = NO;
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:#"page1.caf"];
//this line added to save the audio file
[audioPlayer.data writeToFile:soundFilePath atomically:YES];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(#"error: %#", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
}
As I said, i can record the audio and then play back the audio while in the same view controller, but when i navigate away from the view and return to it I can't playback the recording.
Firstly the play and stop buttons were disabled, which i thought should have only happen when the view was first loaded - but even when i enabled them and pressed the play button, there was no audio.
My other methods are below for my play, stop and record button.
- (IBAction)playAudio:(id)sender {
if (!audioRecorder.recording)
{
btnStop.enabled = YES;
btnRecord.enabled = NO;
//if (audioPlayer)
//[audioPlayer release];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioRecorder.url
error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(#"Error: %#",
[error localizedDescription]);
else
[audioPlayer play];
}
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
btnRecord.enabled = YES;
btnStop.enabled = NO;
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(#"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
NSLog(#"Encode Error occurred");
}
- (IBAction)recordAudio:(id)sender {
if (!audioRecorder.recording)
{
btnPlay.enabled = NO;
btnStop.enabled = YES;
[audioRecorder record];
}
}
- (IBAction)stopAudio:(id)sender {
btnStop.enabled = NO;
btnPlay.enabled = YES;
btnRecord.enabled = YES;
if (audioRecorder.recording)
{
[audioRecorder stop];
} else if (audioPlayer.playing) {
[audioPlayer stop];
}
}
I know there's a lot of code to look at there but if i'm missing out anything please let me know. I'd really appreciate any help with this, thanks.
EDIT 1: Ok, i've done a bit more investigating and opened up the folder where my audio files are saved. The audio records fine and the file size is around 400KB. When i push to the next view controller the filesize remains at 400KB. But as soon as i press my back button (not the preprogrammed one in the nav bar) the filesize goes to 4KB; an empty file. The code for my back button is simply:
- (IBAction)backPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
Please help!
EDIT 2: It also happens with the back button in the nav bar too.
You're allocating audioPlayer with audioRecorder.url. If something happend to audioRecorder it's url might be invalid.
Try setting audioPlayer this way:
- (IBAction)playAudio:(id)sender {
if (!audioRecorder.recording)
{
btnStop.enabled = YES;
btnRecord.enabled = NO;
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:#"page1.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] nitWithContentsOfURL:soundFileUR
error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(#"Error: %#",[error localizedDescription]);
else
[audioPlayer play];
}
}

how to record voice and play in iphone

I have implemented voice recording functionality in our project but i couldn't have control on stop and discard buttons. In second time not able to record the voice after clicking the discard button.
After clicking discard also the audio file is playing and not able to click start button again.
Please help on this one
Here is the source code
.h file
#interface VoiceInput : UIViewController
<AVAudioRecorderDelegate>
{
//Audio record
float remainingDelayTime;
float remainingRecordTime;
UILabel *delayLabel;
UIProgressView *progressView;
AVAudioRecorder *recorder;
NSTimer *delayTimer;
NSTimer *recordTimer;
BOOL toggle;
NSURL *recordedTmpFile;
NSError *error;
}
#property (nonatomic, retain) AVAudioRecorder *recorder;
#property (nonatomic, retain) NSTimer *delayTimer;
#property (nonatomic, retain) NSTimer *recordTimer;
#end
.m file
#implementation VoiceInput
#synthesize progressView;
#synthesize recorder;
#synthesize delayTimer;
#synthesize recordTimer;
- (void)viewDidLoad
{
toggle = YES;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioSession setActive:YES error:&error];
}
-(void)startPushed
{
if (toggle)
{
remainingDelayTime = 4.0;
delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(delayTimerFired:)
userInfo:nil
repeats:YES];
toggle = NO;
NSMutableDictionary *rs = [[NSMutableDictionary alloc] init];
[rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"%.0f.%#", [NSDate timeIntervalSinceReferenceDate] * 1000.0, #"caf"]]];
NSLog(#"USING FILE CALLED: %#", recordedTmpFile);
recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else
{
toggle = YES;
NSLog(#"Using File Called: %#", recordedTmpFile);
[recorder stop];
}
self.discardButton.enabled = NO;
self.startButton.enabled = NO;
self.stopButton.enabled = YES;
}
-(void)stopPushed
{
if([self.recorder isRecording])
{
[self.recorder stop];
if(remainingRecordTime >= 1.0)
{
[self.recordTimer invalidate];
}
}
self.delayLabel.textColor = [UIColor darkGrayColor];
self.delayLabel.text = [[NSString alloc] initWithFormat:#"Record in ..."];
self.discardButton.enabled = YES;
self.playbackButton.enabled = YES;
self.startButton.enabled = NO;
self.stopButton.enabled = NO;
}
-(void)playbackPushed
{
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[avPlayer prepareToPlay];
[avPlayer play];
self.discardButton.enabled = YES;
self.playbackButton.enabled = YES;
self.returnButton.enabled = YES;
self.startButton.enabled = YES;
self.stopButton.enabled = YES;
}
-(void)discardPushed
{
[self.recorder deleteRecording];
self.progressView.progress = 0;
self.delayLabel.textColor = [UIColor darkGrayColor];
self.delayLabel.text = #"Record in ...";
self.discardButton.enabled = NO;
self.playbackButton.enabled = NO;
self.startButton.enabled = YES;
self.stopButton.enabled = YES;
}
-(void)delayTimerFired:(NSTimer *)theDelayTimer
{
self.progressView.progress = 0;
remainingDelayTime -= 1.0;
NSLog(#"fired %f", remainingDelayTime);
self.delayLabel.textColor = [UIColor blackColor];
self.delayLabel.text = [[NSString alloc] initWithFormat:#"Record in %2.0f",
remainingDelayTime];
if(remainingDelayTime <= 0.0)
{
[self.delayTimer invalidate];
self.delayLabel.text = [[NSString alloc] initWithFormat:#"Recording"];
[self.recorder recordForDuration:TIME];
remainingRecordTime = TIME;
recordTimer = [NSTimer scheduledTimerWithTimeInterval:TIME_DECREMENT
target:self
selector:#selector(recordTimerFired:)
userInfo:nil
repeats:YES];
}
}
-(void)recordTimerFired:(NSTimer *)theRecordTimer
{
remainingRecordTime -= TIME_DECREMENT;
NSLog(#"fired %f", remainingRecordTime);
self.progressView.progress = (TIME - remainingRecordTime)/TIME;
if(remainingRecordTime <= 0.0)
{
[self.recordTimer invalidate];
}
}
Thanks in advance
Have a look on this.You can Start recording by this :-
- (void) startRecording
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
[audioSession setMode:AVAudioSessionModeVoiceChat error:&err];
if(err)
{
NSLog(#"audioSession: %# %d %#", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err)
{
NSLog(#"audioSession: %# %d %#", [err domain], [err code], [[err userInfo] description]);
return;
}
recordSetting = [[NSMutableDictionary alloc] init];
// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSetting setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setObject:[NSNumber numberWithInt: AVAudioQualityMax] forKey: AVEncoderAudioQualityKey];
NSString *str;
str = [NSString stringWithFormat:#"%#/MySound.caf",DOCUMENTS_FOLDER];
NSLog(#"recorderFilePath: %#",str);
NSURL *url = [NSURL fileURLWithPath:str];
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder)
{
NSLog(#"recorder: %# %d %#", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: #"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
return;
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: #"Warning"
message: #"Audio input hardware not available"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
return;
}
// start recording
[recorder recordForDuration:(NSTimeInterval) 20];
}
This will start your recording.After it you can stop recording :-
- (void) stopRecording
{
[recorder stop];
}
Now you can play your recording by this :-
- (void)playRecordingSound
{
if(!recorderFilePath)
recorderFilePath = [NSString stringWithFormat:#"%#/MySound.caf", DOCUMENTS_FOLDER] ;
if(soundID)
{
AudioServicesDisposeSystemSoundID(soundID);
}
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:recorderFilePath isDirectory:NO];
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);
}
Hope it helps Thanks :)

Why is AVAudioRecorder prepareToRecord Failing?

I am trying to setup a basic controller that will record user audio input(voice). However, the AVAudioRecorder's prepareToRecord method is failing and I can't figure out why. I have setup the audio session in my app delegate and I do not receive an errors when I instantiate the AVAudioRecorder instance:
// App delegate snippet
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
NSError* audioSessionError = nil;
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord
error: &audioSessionError];
if (audioSessionError) {
NSLog (#"Error setting audio category: %#", [audioSessionError localizedDescription]);
} else {
NSLog(#"No session errors for setting category");
}
[audioSession setActive:YES error:&audioSessionError];
if (audioSessionError) {
NSLog (#"Error activating audio session: %#", [audioSessionError localizedDescription]);
} else {
NSLog(#"no session errors for setActive");
}
// VIEW DID LOAD IN RECORDERCONTROLLER
- (void)viewDidLoad {
self.navigationItem.title = [NSString stringWithFormat:#"%#", [[MyAppDelegate loadApplicationPlist] valueForKey:#"recorderViewTitle"]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(dismiss)];
[self alertIfNoAudioInput];
[self createAVAudioRecorder];
minutesSecondsFormatter = [[SimpleMinutesSecondsFormatter alloc] init];
currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self selector:#selector(updateAudioDisplay)
userInfo:NULL repeats:YES];
[super viewDidLoad];
}
// CREATE AVAUDIORECORDER
- (NSError *)createAVAudioRecorder {
NSError *recorderSetupError = nil;
[audioRecorder release];
audioRecorder = nil;
NSString *timestamp = [NSString stringWithFormat:#"%d", (long)[[NSDate date] timeIntervalSince1970]];
NSString *destinationString = [[MyAppDelegate getAppDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.caf", timestamp]];
NSLog(#"destinationString: %#", destinationString);
NSURL *destinationUrl = [NSURL fileURLWithPath: destinationString];
audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationUrl
settings:[[AVRecordSettings sharedInstance] getSettings]
error:&recorderSetupError];
if (recorderSetupError) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle:#"Can't record"
message:[recorderSetupError localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
[cantRecordAlert release];
return recorderSetupError;
} else {
NSLog(#"no av setup error");
}
if ([audioRecorder prepareToRecord]) {
recordPauseButton.enabled = YES;
audioRecorder.delegate = self;
} else {
NSLog(#"couldn't prepare to record");
}
NSLog (#"recorderSetupError: %#", recorderSetupError);
return recorderSetupError;
}
The prepareToRecord also fails (silently, without an error) if the directory where you try to save the file doesn't exist. Use NSFileManager to check if the directory already exists.
It is failing because you did not initialize the AVAudioRecorder object using proper settings. Do this before initializing it:
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
then you can instantiate it using
audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationUrl
settings:recordSettings
error:&recorderSetupError];