简体   繁体   English

在真实设备中录制音频不起作用

[英]Recording audio in real device doesn't work

I'm implementing the audio recording in my app, i've tried the code i've wrote in the simulator and all work good, but when i try in the real device when i check the audio record duration i se that it's always zero, but in the simulator it shows the correct duration and the code is the same. 我正在我的应用程序中实现音频记录,我已经尝试了在模拟器中编写的代码,并且一切正常,但是当我在真实设备中尝试检查音频记录持续时间时,我发现它始终为零,但在模拟器中显示正确的持续时间,并且代码相同。 I've to set something to use record in real device? 我必须设置一些要在真实设备中使用的记录吗?

I have an Excellent recording method: 我有一个很棒的录音方法:

-(IBAction) startRecording
{   progressView.progress = 0.0;

NSLog(@"startRecording");
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];
err = nil;
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

recordSetting = [[NSMutableDictionary alloc] init];

// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];

// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

// These settings are used if we are using kAudioFormatLinearPCM format
//[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
//[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];



// Create a new dated file
//NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
//  NSString *caldate = [now description];
//  recorderFilePath = [[NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate] retain];
recorderFilePath = [NSString stringWithFormat:@"%@/MySound.caf", DOCUMENTS_FOLDER] ;

NSLog(@"recorderFilePath: %@",recorderFilePath);
_recorderFilePath = recorderFilePath;
self.recorderFilePath = recorderFilePath;
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];

err = nil;

NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(audioData)
{
    NSFileManager *fm = [NSFileManager defaultManager];
    [fm removeItemAtPath:[url path] error:&err];
}

err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
    NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: [err localizedDescription]
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];
     return;
}

//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;

BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
    UIAlertView *cantRecordAlert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: @"Audio input hardware not available"
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [cantRecordAlert show];
     return;
}

// start recording
[recorder recordForDuration:(NSTimeInterval) 30];

lblStatusMsg.text = @"Recording...";
progressView.progress = 0.0;
timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
NSLog(@"recording");
startRecord.enabled = NO;
stopRecord.enabled = YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM