首页 文章

AVAudioSession会自动重新路由iPhone音频吗?

提问于
浏览
1

在我正在研究的cocos2d游戏中使用AVAudioRecorder我遇到了AVAudioSession的问题 .

我正在尝试使用简单的AVAudioRecorder示例捕获麦克风输入,以检测用户何时在麦克风中发出声音(声音本身无关紧要,因为我正在录制到/ dev / null) .

这是我的麦克风设置代码:

NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"];

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];

    NSDictionary *recordSettings =
    [[NSDictionary alloc] initWithObjectsAndKeys:
     [NSNumber numberWithFloat: 22050.0], AVSampleRateKey,
     [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
     [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
     [NSNumber numberWithInt: AVAudioQualityLow],
     AVEncoderAudioQualityKey,
     nil];

    micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil];
    [newURL release];
    [recordSettings release];
    [micInput setMeteringEnabled:YES];

在具有上述代码的iPhone上,场景开始时所有音频(音效,背景音乐等)都以非常低的水平播放,因为它仅通过手机扬声器而不是外部扬声器播放 . 当我在iPad或iPod Touch上测试时,背景音频按预期通过外部扬声器播放 . 这是一个问题,因为在游戏的这个特定部分播放iPhone版本时,游戏的体积会大幅降低 .

当我注释掉AVAudioSession设置线时,声音通过外部扬声器播放,但当然我再也无法获得麦克风输入 . 这个问题有解决方法或解决方案吗?我需要能够使用AVAudioRecorder进行录制,但仍然可以从iPhone的外部扬声器输出音频 .

谢谢!

1 回答

  • 2

    在设置音频会话后尝试以下内容:

    UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
             AudioSessionSetProperty (
               kAudioSessionProperty_OverrideAudioRoute,
               sizeof (ASRoute),
               &ASRoute
             );
    

相关问题