繁体   English   中英

在AVAudioRecorder和AVAudioPlayer之间配置

[英]Conflit between AVAudioRecorder and AVAudioPlayer

这是我的问题:

代码(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的所有部分,它将起作用。 我认为存在渠道问题。

谢谢

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM