2013-04-23 4 views
0

Я использую аудиозапись в своем приложении, я пробовал код, который я написал в симуляторе, и все работает хорошо, но когда я пытаюсь в реальном устройстве, когда Я проверяю продолжительность аудиозаписи, так что она всегда равна нулю, но в симуляторе она показывает правильную продолжительность и код тот же. Я должен установить что-нибудь, чтобы использовать запись в реальном устройстве?Запись звука в реальном устройстве не работает

+0

вы смогли выяснить, почему это происходит? , Столкнувшись с тем же вопросом – vijar

ответ

1

У меня есть отличный способ записи:

-(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; 
} 
+0

, вы пробовали этот метод в реальном ipad, и он работает? – user1575803

+0

Да, я сделал, его полностью функциональный – Mutawe

+0

У меня такая же проблема, есть проблема с записью звука, но когда я пытаюсь его в симуляторе, он работает – user1575803

Смежные вопросы