1
我在使用AVAudioRecorder的AVAudioSession在我正在开发的cocos2d游戏中遇到问题。AVAudioSession自动改变iPhone音频?
我试图捕捉话筒输入使用一个简单的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的外部扬声器输出音频。
谢谢!
这个解决我的问题。非常感谢您的帮助! – chmod