【问题标题】:Conflit between AVAudioRecorder and AVAudioPlayerAVAudioRecorder 和 AVAudioPlayer 之间的冲突
【发布时间】:2010-04-05 15:11:31
【问题描述】:

这是我的问题:

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

谢谢

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 不要抑制错误返回。任何有方法的方法都可能以某种方式失败;发生这种情况时,您将想知道它是如何失败的,以便您可以防止它失败或从失败中优雅地恢复。
    • 干得好!这个解决方案也对我有用。但正如彼得所说,如果您遇到问题,请不要隐藏错误。
    • 为什么要保留单例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 2012-06-27
    • 2012-03-06
    • 2011-05-02
    • 2012-11-25
    • 2013-10-27
    • 2014-03-02
    相关资源
    最近更新 更多