我正在开发直播应用。我在iPhone 6s和6s Plus上都存在音频失真问题。我知道Apple已经改变了音频硬件采样率(达到48000Hz)。
我尝试过以下代码,但音频仍然失真:
-(void) setupAudioSpeaker
{
NSError * audioSessionError = nil;
[[AVAudioSession sharedInstance] setCategory:
AVAudioSessionCategoryPlayback error: &audioSessionError];
if (audioSessionError) {
DLog(@"AVAudioSession Error %ld, %@",
(long)audioSessionError.code,
audioSessionError.localizedDescription);
}
NSTimeInterval bufferDuration =0.01;
[[AVAudioSession sharedInstance]
setPreferredIOBufferDuration:bufferDuration
error:&audioSessionError];
if (audioSessionError) {
DLog(@"Error %ld, %@", (long)audioSessionError.code,
audioSessionError.localizedDescription);
}
double sampleRate = 48000.0;
[[AVAudioSession sharedInstance] setPreferredSampleRate:sampleRate
error:&audioSessionError];
if (audioSessionError) {
DLog(@"AVAudioSession Error %ld, %@",
(long)audioSessionError.code,
audioSessionError.localizedDescription);
}
[[AVAudioSession sharedInstance] setActive:YES
error:&audioSessionError];
if (audioSessionError) {
DLog(@"AVAudioSession Error %ld, %@", (long)audioSessionError.code,
audioSessionError.localizedDescription);
}
sampleRate = [AVAudioSession sharedInstance].sampleRate;
bufferDuration = [AVAudioSession sharedInstance].IOBufferDuration;
DLog(@"Sampe Rate:%0.0fHZ I/O Buffer Duration:%f", sampleRate,
bufferDuration);
}