3

我正在为 iphone 6+ 开发录音应用程序。
问题1:(AVAudioRecorder)音频录制在模拟器中工作正常,但在设备中不工作..

音频设置:

[settings setValue:[NSNumber numberWithInteger:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

[settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];

[settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey];

[settings setValue:[NSNumber numberWithInteger:AVAudioQualityLow] forKey:AVEncoderAudioQualityKey];

问题 2:在我的 ipad 中的麦克风工作正常之前。但是当我使用这段代码时

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
 [audioSession setActive:YES error:&err];

麦克风在 ipad 中不起作用.. 如何在 ipad/iphone 中重置或获取我的麦克风电平

4

1 回答 1

1

在我的- (void)setUpAudio方法中,我创建了一个字典,其中包含 AVAudioRecorder 的设置。(它有点干净)您上面的代码几乎是正确的,但并不完全正确。见下文。

// empty URL
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

// define settings for AVAudioRecorder
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                      AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless],      AVFormatIDKey,
                          [NSNumber numberWithInt:1],                               AVNumberOfChannelsKey,
                          [NSNumber numberWithInt:AVAudioQualityMax],               AVEncoderAudioQualityKey,
                          nil];

NSError *error;

// init and apply settings
 recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

// This here is what you are missing, without it the mic input will work in the simulator, 
// but not on a device.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
                    error:nil];

if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    NSTimer *levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES];
    [recorder record];
} else {
    NSLog([error description]);
}

然后在更新方法中,您可以像这样跟踪您的麦克风输入电平。

- (void)levelTimerCallback:(NSTimer *)timer {
    [recorder updateMeters];
    NSLog(@"Average input: %f Peak input: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);

} 

任何问题或我可以改进答案的方法,请告诉我。这里还是很新的。

于 2013-12-09T03:54:13.030 回答