1

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];

and

[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.

Thanks

1 Answer 1

6

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];
3
  • Don't suppress error returns. Any method that has one can fail in some way; when that happens, you will want to know how it failed, so that you can prevent it from failing or recover from the failure gracefully. Apr 6, 2010 at 12:08
  • Great work! This solution worked for me too. But as Peter said, if you're having problems, don't suppress the errors.
    – epetousis
    Mar 11, 2012 at 8:38
  • 1
    Why are you retaining a singleton?
    – Tudor
    Mar 16, 2012 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.