【问题标题】:How to record audio in objective c for iphone app in .amr format?如何以 .amr 格式为 iphone 应用程序录制目标 c 中的音频?
【发布时间】:2015-02-23 05:32:17
【问题描述】:

我使用以下代码录制音频。它适用于 .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 格式录制和保存音频?

【问题讨论】:

标签: ios iphone audio amr


【解决方案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...");

【讨论】:

  • 我已经编辑了我的代码。此代码运行良好,我将以 .3GP 格式录制音频..
猜你喜欢
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
相关资源
最近更新 更多