Как записать звук в приложении Objective C для iphone в формате .amr?

Я использовал следующий код для записи звука. Он отлично работает с форматом .caf.

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];

Но для .amr, если я изменю расширение на .amr, как показано ниже, у меня не получится записывать звук.

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

может ли кто-нибудь научить меня, как записывать и сохранять аудио в формате .amr?


person hardik hadwani    schedule 23.02.2015    source источник
comment
stackoverflow.com/questions/276644/   -  person hardik hadwani    schedule 23.02.2015


Ответы (1)


попробуйте следующий код, может быть вам полезно ..

AppDelegate *del = (AppDelegate*)[[UIApplication sharedApplication]delegate];
//set path & name for Recording Audio
                NSString *dataPath=[del.path_Folder_Document copy];
                NSString *filePath = [NSString stringWithFormat:@"%@/Images_Audio/%@.3gp", dataPath,[ARR_Img_File_Name_Moment objectAtIndex:pos]];
                NSLog(@"Audio File Path =%@",filePath);

                NSURL *tmpFileUrl = [NSURL fileURLWithPath:filePath];
//set Record Setting 
                NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                                [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                                [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                                nil];

                NSError *error = nil;
//Prepare Recorder to Start Recording Audio
                recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
                recorder.delegate = self;
                [recorder prepareToRecord];

                AVAudioSession *session = [AVAudioSession sharedInstance];
                [session setCategory:AVAudioSessionCategoryRecord error:nil];
                [session setActive:YES error:nil];
//Start Recording Audio ..

                if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
                    NSLog(@"Writable Pathforimage_Audio 1st>> %@", filePath);

                }else
                {
                    NSLog(@"Not Writable");
                }

                [recorder record];

                NSLog(@"its recording audio...");
person Mehul    schedule 23.02.2015
comment
Я отредактировал свой код. этот код работает отлично, я буду записывать аудио в формате .3GP .. - person Mehul; 23.02.2015