录制声音文件后,这只能在iPad Air中听不到 [英] After record sound file, this can't heard in iPad Air only

查看:156
本文介绍了录制声音文件后,这只能在iPad Air中听不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Apple Store中有一个具有录音功能的应用程序。我迁移到Xcode 5.0.2和SDK 7.
除iOS Air外,iOS 6或7设备的此功能没有问题。
这是我的记录声音例程:

I have an app in the Apple Store with voice recording capability. I migrate to Xcode 5.0.2 and SDK 7. I have no problem with this feature in devices with iOS 6 or 7 except with iPad Air. This is my record Sound routine:

+(void) startRecording:(NSString *)fileName :(NSString *)fileType
{   NSError* theError = nil;
BOOL result = YES;

// Init audio with record capability
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
result = [audioSession setCategory:AVAudioSessionCategoryRecord error:&theError];
if (!result)
{
    NSLog(@"setCategory failed %@", theError);
}
result = [audioSession setActive:YES error:nil];
if (!result)
{
    NSLog(@"setActive failed %@", theError);
}

// Verify if you have granted to use microphone. iOS 7 or later
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
    [audioSession requestRecordPermission:^(BOOL granted) {
        if (granted) {
            // Record Sound
            [self recordNow:fileName :fileType];
        }
        else {
            // Microphone disabled code
            NSLog(@"Microphone is disabled..");

            // We're in a background thread here, so jump to main thread to do UI work.
            dispatch_async(dispatch_get_main_queue(), ^{
                [[[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                             message:@"This app requires access to your device's Microphone.\n\nPlease enable Microphone access for this app in Settings / Privacy / Microphone"
                                            delegate:nil
                                   cancelButtonTitle:@"Dismiss"
                                   otherButtonTitles:nil] show];
            });
        }
    }];
}
else {  // iOS 6 
    // Record sound
    [self recordNow:fileName :fileType];
}
}

// Record Sound
+(void) recordNow:(NSString *)fileName :(NSString *)fileType {
recordEncoding = ENC_AAC;

// Microphone enabled code
if (kDebugMode) {
    NSLog(@"Microphone is enabled..");
}

NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
if(recordEncoding == ENC_PCM)
{
    [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
}
else
{
    NSNumber *formatObject;

    switch (recordEncoding) {
        case (ENC_AAC):
            formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
            break;
        case (ENC_ALAC):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
            break;
        case (ENC_IMA4):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
            break;
        case (ENC_ILBC):
            formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
            break;
        case (ENC_ULAW):
            formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
            break;
        default:
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
    }

    [recordSettings setObject:formatObject forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVEncoderBitRateKey];
    [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey:         AVEncoderAudioQualityKey];
}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recDir = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.%@", recDir, fileName, fileType]];    
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];

[audioRecorder recordForDuration:[[NSUserDefaults standardUserDefaults] integerForKey:kRecordForDuration]];

}


推荐答案

在这个网站内进行了很多研究,我找到了一个很好的评论,可以节省我的一天:
现在我已经删除了AVEncoderBitRateKey和值,它也适用于5S。

After a lot research inside this site, i found an excellent comment that saves my day here: "Now that I've removed the AVEncoderBitRateKey and value, it also works on the 5S."

所以,我只是评论这个行并修复它!

So, i just comment this row and fix it !!

[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVEncoderBitRateKey];

我可以再次听到iPad Air中的录音!
为什么会这样?我还不知道,但它的作品也许你有同样的问题,只想在这个网站上留下关于这个案例的日志。

I can hear again the record sound in iPad Air ! Why happen this? I did not know yet, but its works and maybe you have the same problem and just want to leave a log about this case in this website.

这篇关于录制声音文件后,这只能在iPad Air中听不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆