Tutorial :Conflit between AVAudioRecorder and AVAudioPlayer


here is my problem :

The code (FooController) :

   NSString *path = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"m4v"];       soundEffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]  error:NULL];  [soundEffect play];      // MicBlow      micBlow = [[MicBlowController alloc]init];  

And MicBlowController contains :

        NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];      NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:                                [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,                                [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,                                [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,                                [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,                                nil];  


[recorder updateMeters];  const double ALPHA = 0.05;  double peakPowerForChannel = pow(10,(0.05*[recorder peakPowerForChannel:0]));  lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;    NSLog(@"Average input: %f Peak input %f Low pass results: %f",[recorder averagePowerForChannel:0],[recorder peakPowerForChannel:0],lowPassResults);  

If I play the background sound and try to get the peak from the mic I get this log : Average input: 0.000000 Peak input -120.000000 Low pass results: 0.000001

But if I comment all parts about AVAudioPlayer it works. I think there is a problem of channel.



Well, I've found the solution and it is AVAudioSession !

By using the method setCategory (with AVAudioSessionCategoryPlayAndRecord as argument) I can play sounds and record sounds from the mic.

audioSession = [[AVAudioSession sharedInstance] retain];  [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];  [audioSession setActive:YES error: nil];  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »