2013-10-10 3 views
0
내가 작동이 코드 사용이나 목소리 사용자를 기록하는 AVAudioRecorder를 사용하는 것을 시도하고있다

: 그것은 내가 사용하는 경우 작동하지 않습니다 그러나AVAudioRecorder 특별한 경우에 기록되지 않습니다

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
date = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
[formatter setDateFormat: @"yyyy-MM-dd ss"]; 
NSString *dateWithString = [formatter stringFromDate:date]; 
NSLog(@"%@",dateWithString); 
NSString *soundFilePath1 = [docsDir 
          stringByAppendingPathComponent:nil]; 
NSString *soundFilePath = [soundFilePath1 
          stringByAppendingPathComponent:@"sound.caf"]; 

NSLog(@"%@",soundFilePath); 

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

NSDictionary *recordSettings = [NSDictionary 
           dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:AVAudioQualityMin], 
           AVEncoderAudioQualityKey, 
           [NSNumber numberWithInt:16], 
           AVEncoderBitRateKey, 
           [NSNumber numberWithInt: 2], 
           AVNumberOfChannelsKey, 
           [NSNumber numberWithFloat:44100.0], 
           AVSampleRateKey, 
           nil]; 

NSError *error2; 

self.recorder = [[AVAudioRecorder alloc] 
        initWithURL:soundFileURL 
        settings:recordSettings 
        error:&error2]; 

을 :

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
date = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
[formatter setDateFormat: @"yyyy-MM-dd ss"]; 
NSString *dateWithString = [formatter stringFromDate:date]; 
NSLog(@"%@",dateWithString); 
NSString *soundFilePath1 = [docsDir 
          stringByAppendingPathComponent:dateWithString]; 
NSString *soundFilePath = [soundFilePath1 
          stringByAppendingPathComponent:@"sound.caf"]; 

NSLog(@"%@",soundFilePath); 

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

NSDictionary *recordSettings = [NSDictionary 
           dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:AVAudioQualityMin], 
           AVEncoderAudioQualityKey, 
           [NSNumber numberWithInt:16], 
           AVEncoderBitRateKey, 
           [NSNumber numberWithInt: 2], 
           AVNumberOfChannelsKey, 
           [NSNumber numberWithFloat:44100.0], 
           AVSampleRateKey, 
           nil]; 

NSError *error2; 



self.recorder = [[AVAudioRecorder alloc] 
        initWithURL:soundFileURL 
        settings:recordSettings 
        error:&error2]; 

코드의 두 블록 사이의 차이는 다음

NSString *soundFilePath1 = [docsDir stringByAppendingPathComponent:dateWithString]; 

왜 이런 일이 발생합니까?

답변

0

이 코드를 사용하여 디렉토리가 제대로 작성되었는지 확인하십시오.

NSArray *dirPaths; 
    NSString *docsDir; 

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSDate *date = [NSDate date]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [formatter setDateFormat: @"yyyy-MM-dd ss"]; 
    NSString *dateWithString = [formatter stringFromDate:date]; 
    NSLog(@"%@",dateWithString); 
    docsDir = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:dateWithString]; 

    NSError *e; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:docsDir]) //Does directory already exist? 
    { 
     if (![[NSFileManager defaultManager] createDirectoryAtPath:docsDir 
             withIntermediateDirectories:NO 
                 attributes:nil 
                  error:&e]) 
     { 
      NSLog(@"Create directory error: %@", e); 
     } 
    } 

즉 적용한 후

,

NSString *soundFilePath = [docsDir 
           stringByAppendingPathComponent:@"sound.caf"]; 

    NSLog(@"%@",soundFilePath); 



    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath isDirectory:NO]; 

    NSDictionary *recordSettings = [NSDictionary 
            dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithInt:AVAudioQualityMin], 
            AVEncoderAudioQualityKey, 
            [NSNumber numberWithInt:16], 
            AVEncoderBitRateKey, 
            [NSNumber numberWithInt: 2], 
            AVNumberOfChannelsKey, 
            [NSNumber numberWithFloat:44100.0], 
            AVSampleRateKey, 
            nil]; 

    NSError *error2; 
0

문서 디렉토리 또는 임시 디렉토리에있는 최고 수준의 음성이

-(IBAction)record:(id)sender 
{ 
NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

NSDate *date = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
[formatter setDateFormat: @"yyyy-MM-dd ss"]; 
NSString *dateWithString = [formatter stringFromDate:date]; 
NSLog(@"%@",dateWithString); 

NSString *dateFolderPath = [docsDir stringByAppendingPathComponent:dateWithString]; 
NSError *error; 
if (![[NSFileManager defaultManager] fileExistsAtPath:dateFolderPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:dateFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder 

NSString *soundFilePath = [dateFolderPath 
          stringByAppendingPathComponent:@"sound.caf"]; 

NSLog(@"%@",soundFilePath); 

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

NSDictionary *recordSettings = [NSDictionary 
           dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:AVAudioQualityMin], 
           AVEncoderAudioQualityKey, 
           [NSNumber numberWithInt:16], 
           AVEncoderBitRateKey, 
           [NSNumber numberWithInt: 2], 
           AVNumberOfChannelsKey, 
           [NSNumber numberWithFloat:44100.0], 
           AVSampleRateKey, 
           nil]; 

NSError *error2; 

recorder = [[AVAudioRecorder alloc] 
      initWithURL:soundFileURL 
      settings:recordSettings 
      error:&error2]; 
[recorder record]; 
} 

-(IBAction)stop:(id)sender 
{ 
    [recorder stop]; 
    NSLog(@"stop"); 
} 
0

AVAudioRecorder 기록을보십시오. 문서 디렉토리에 생성 된 폴더에 오디오를 녹음하려고하면 실패합니다. 임시 디렉토리에 항상 오디오를 녹음하고 녹음 후 문서 디렉토리의 폴더로 파일을 이동하십시오.

관련 문제