2011-08-01 3 views
0

안녕하세요, 선량을 녹음하는 코드가 있지만 저장 방법을 알아 내지 못하고 다시 돌아올 때 동일한 녹음 파일을 다시 재생할 수 있습니까?iPhone에 녹음 한 후 오디오 파일을 저장하는 방법은 무엇입니까?

나는 녹음 코드 아래 사용하고 -

----- 
-(IBAction) startRecording 
{ 
    NSLog(@"startRecording"); 
    [audioRecorder release]; 
    audioRecorder = nil; 

    // Init audio with record capability 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; 

    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:12800] forKey:AVEncoderBitRateKey]; 
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey]; 
} 

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]]; 


NSError *error = nil; 
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error]; 

if ([audioRecorder prepareToRecord] == YES){ 
    [audioRecorder record]; 
} else { 
    int errorCode = CFSwapInt32HostToBig ([error code]); 
    NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); 

} 
NSLog(@"recording"); 
} 
---- 

내가 어떻게이 오디오 파일을 저장할 수 있음을 알려 주시기 바랍니다? 그래서 다시 응용 프로그램을 열고 녹음 된 파일을 재생할 수 있습니다.

감사합니다.

답변

1

저장하려는 파일의 이름에 유효한 경로를 추가하십시오.

NSURL *url = [NSURL fileURLWithPath:recordFile]; 
:의 URL을 얻을 수

NSString *recordFile = [recordDir stringByAppendingPathComponent: @"recordTest.caf"]; 

사용이 전체 경로 :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *recordDir = [paths objectAtIndex:0]; 

파일 이름이 recordDir을 추가 : 예를 들어 응용 프로그램의 문서 디렉토리의 쓰기 가능한 경로를 가져올 수

이제이 URL을 사용하여 AVAudioRecorder을 만드십시오. ViewController.m에서

0
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"Recording.aiff"]; 

[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:path] completionBlock:^(NSURL *assetURL, NSError *error){ 

    /*notify of completion*/ 

    NSLog(@"AssetURL: %@",assetURL); 
    NSLog(@"Error: %@",error); 

    if (!error) { 
     //video saved 

     [[self appDelegate] showAlertWithTitle:@"Audio Saved" message:@""]; 

    } 
    else{ 

     [[self appDelegate] showAlertWithTitle:@"Error" message:error.domain]; 

    } 

}]; 
0

청소 viewLoad : - : ViewController.m에

- (IBAction) record 
{ 

NSError *error; 

// Recording settings 
NSMutableDictionary *settings = [NSMutableDictionary dictionary]; 

[settings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; 
[settings setValue: [NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; 
[settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; 
[settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
[settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
[settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 
[settings setValue: [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey]; 

NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentPath_ = [searchPaths objectAtIndex: 0]; 

NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]]; 

// File URL 
NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH]; 


//Save recording path to preferences 
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 


[prefs setURL:url forKey:@"Test1"]; 
[prefs synchronize]; 


// Create recorder 
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; 

[recorder prepareToRecord]; 

[recorder record]; 
} 

편집 재생 (무효)은 ViewController.m의 {

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

[audioSession setActive:YES error:nil]; 

[recorder setDelegate:self]; 

[super viewDidLoad]; 
} 

편집 기록을의 viewDidLoad :

-(IBAction)playBack 
{ 

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; 

[audioSession setActive:YES error:nil]; 


//Load recording path from preferences 
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 


temporaryRecFile = [prefs URLForKey:@"Test1"]; 



    player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; 



    player.delegate = self; 


    [player setNumberOfLoops:0]; 
    player.volume = 1; 


[player prepareToPlay]; 

[player play]; 


} 
,

및 ViewController.m에 새 날짜 열 방법을 추가 :

- (NSString *) dateString 
{ 
// return a formatted string for a file name 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
formatter.dateFormat = @"ddMMMYY_hhmmssa"; 
return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"]; 
} 

는 이제 그것을로드 NSUserdefaults를 통해 가장 최근에 녹화 된 파일을로드 할 수 있습니다 : (IBAction를) 재생에

//Load recording path from preferences 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 


    temporaryRecFile = [prefs URLForKey:@"Test1"]; 



    player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; 

합니다. temporaryRecFile은 ViewController 클래스의 NSURL 변수입니다. 다음과 같은 선언

ViewController.h :

@interface SoundRecViewController : UIViewController <AVAudioSessionDelegate,AVAudioRecorderDelegate, AVAudioPlayerDelegate> 
{ 
...... 
...... 
NSURL *temporaryRecFile; 

AVAudioRecorder *recorder; 
AVAudioPlayer *player; 

} 
...... 
...... 
    @end 
관련 문제