1

这是我的问题:

代码(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 的所有部分,它就可以工作。我觉得渠道有问题。

谢谢

4

1 回答 1

6

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

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

audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive:YES error: nil];
于 2010-04-05T20:32:56.520 回答