【问题标题】:AvAudiorecorder not functioning properly in iOS 5.1.1 and iPhone4AvAudiorecorder 在 iOS 5.1.1 和 iPhone4 中无法正常运行
【发布时间】:2013-03-06 09:54:58
【问题描述】:

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

-(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 应用程序两个应用运行良好,这意味着硬件运行良好。

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

【问题讨论】:

    标签: iphone ios objective-c ipad avaudiorecorder


    【解决方案1】:

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

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

     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];
    

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 2011-06-30
      • 2019-11-08
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多