1

我正在开发我的应用程序中的录音功能。我正在使用这段代码:

-(void)startAudioRecorder:(NSError**)error{
    
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }
    [audioSession setActive:YES error:&err];
    
        NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init] ;
        [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//kAudioFormatAppleIMA4,kAudioFormatAAC
        [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    
    NSString *absolutePath=[self getAbsoluteAudioFilePath];
    if([[NSFileManager defaultManager] fileExistsAtPath:absolutePath]){
       // [[NSFileManager defaultManager ] removeItemAtPath:absolutePath error:nil];
    }
    NSError *audioRecorderError=nil;
    NSURL *absoluteUrl=[NSURL fileURLWithPath:absolutePath];
    
    self.audioRecorder=[[[AVAudioRecorder alloc] initWithURL:absoluteUrl settings:recordSetting error:&audioRecorderError] autorelease];
    NSLog(@"%s audioRecorder created=%@",__func__,self.audioRecorder);
    
    [self.audioRecorder setDelegate: self];
    BOOL prepared=[self.audioRecorder prepareToRecord];
    BOOL record=[self.audioRecorder record];
    
    if(!prepared || !record){
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"message" message:@"something wrong" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    *error=audioRecorderError;
    //[recordSetting release];
    //recordSetting=nil;
    
    
}

从各种博客中,我找到了上面的录制代码,它在所有 iOS 设备上都按预期工作,我已经在 iPad、iPod 和 iPhone 上进行了测试。不幸的是,上面的代码在iPhone 4、iOS 5.1.1设备中以非常少的声音录制,其余所有组合都很好,我已经在语音备忘录中打开并录制了我的声音,同一硬件设备的 SpeakEasy 应用程序这两个应用程序运行良好,这意味着硬件运作良好。

是否有任何修改可以解决我的问题?录制高质量声音的最佳方法是什么?

4

1 回答 1

0

(代表问题作者发布解决方案,将其移至答案部分)

在我的方法中添加了以下行来解决问题

 NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    //Set the general audio session category
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryErr];
    
    //Make the default sound route for the session be to use the speaker
    UInt32 doChangeDefaultRoute = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
    
    //Activate the customized audio session
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
于 2021-08-15T11:06:15.553 回答