开发者

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 num开发者_高级运维berWithInt: 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜