尝试在iPhone中录制音频时音质不佳

时间:2011-07-11 10:54:47

标签: iphone ipad avaudioplayer avaudiorecorder avaudiosession

我正在使用AVAudioRecorder类在我的应用中录制用户输入语音。我能够录制和播放声音。但是,产量的质量非常低。在尝试播放录制的音频时,我在背景中收到很多噪音。 我该怎么做才能获得丰富的音质输出。我正在使用以下设置(代码)来录制声音。

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; // previously i'v used 8400
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

以及播放音频的以下代码

[[AVAudioSession sharedInstance] setCategory:
 AVAudioSessionCategoryPlayback error:NULL];

NSURL *fileTobePlayed = [NSURL fileURLWithPath:[[[[UIApplication sharedApplication]delegate] applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",currentPage]]];
//AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileTobePlayed error:&error];

NSLog(@"The file being played is %@",fileTobePlayed);
[audioPlayer prepareToPlay];
[audioPlayer play];
audioPlayer.volume = 4.0;

谢谢..

1 个答案:

答案 0 :(得分:2)

使用此代码添加它。

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;     

            AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,          
                                     sizeof (audioRouteOverride),&audioRouteOverride);