AVAudioRecorder和AVAudioPlayer之间的冲突

时间:2010-04-05 15:11:32

标签: iphone objective-c cocoa-touch avaudioplayer avaudiorecorder

这是我的问题:

代码(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];

MicBlowController包含:

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

如果我播放背景音并尝试从麦克风获得峰值,我会得到这个日志: 平均输入:0.000000峰值输入-120.000000低通结果:0.000001

但是,如果我评论有关AVAudioPlayer的所有部分,它的确有效。我认为存在渠道问题。

由于

1 个答案:

答案 0 :(得分:6)

好吧,我找到了解决方案,它是AVAudioSession!

通过使用方法setCategory(使用AVAudioSessionCategoryPlayAndRecord作为参数),我可以播放声音并从麦克风录制声音。

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