2011-04-12 6 views
0

저는 AV 오디오 프레임 워크를 사용하여 iPhone에 오디오 파일을 녹음하고 있습니다.객관적인 C (iPhone)로 녹음 된 오디오를 저장하는 방법

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(
               NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
NSString *soundFilePath = [docsDir 
          stringByAppendingPathComponent:@"sound.caf"]; 

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 *error = nil; 

audioRecorder = [[AVAudioRecorder alloc] 
       initWithURL:soundFileURL 
       settings:recordSettings 
       error:&error]; 

가 지금은 다른 기능을 가지고 있고 내가 좋아하는 그 기능이 무엇인가에 sound.caf 파일을 보낼 : 여기에 현재 코드

NSData *soundData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:soundFilePath ofType:@"caf"]]; 

그러나 어떤 이유로 그 위 라인은 0kb 파일을 반환하는 것 같습니다.

제안 사항? http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html

을도 아니다 동일한 파일 이름에 개방 파일 포인트 :

+0

이 질문은하지 개발 환경의 엑스 코드에 대해, iOS 및 자사의 API에 대한 것입니다 : 당신은 당신이 새로운 오디오 세션을 시작하는 모두의

먼저 주어진 코드를 사용해보십시오. 그에 따라 제목과 태그를 업데이트하십시오. – Codo

+0

파일의 sound.caf가 프로젝트의 Resources 폴더에 있는지 확인하십시오. – visakh7

+0

안녕하세요, 그렇습니다. 문제가 발견되었습니다. 누구든지 같은 문제가 발생하면 다음 줄을 사용하여 사운드 파일을 가져올 수 있습니다. audioRecorder.url Nomad

답변

0

나는 당신이 기록 방법을 트리거 표시되지 않습니다.

마지막으로 앱 디렉토리에 저장할 수 없습니다. 문서 폴더에만 데이터를 저장할 수 있습니다.

1

코드에서 파일 URL은 전역 적이 아닙니다.

-(void) initAudioSession { 

    AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 

     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; 

     [audioSession setActive:YES error: &error]; 
} 

-(void) recordStart { 

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *docsDir = [dirPaths objectAtIndex:0]; 

    tempFilePath = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]]; 


    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init]; 
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey]; 
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; 
stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]]; 


    recorder = [[ AVAudioRecorder alloc] initWithURL:tempFilePath settings:recordSetting error:&error]; 
    [recorder setDelegate:self]; 

    [recorder prepareToRecord]; 

    [recorder record]; 
} 

-(void) stopRecording{ 
    [recorder stop]; 
} 

-(void) playRecord{ 
    avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:tempFilePath error:&error]; 
    [avPlayer prepareToPlay]; 
    [avPlayer play]; 
} 
관련 문제