iphone - iOS:录音文件格式

标签 iphone objective-c ios xcode audio-recording

我正在实现录音。它适用于 cafwave 格式。但问题是文件太大。

所以,谁能帮我录制同样在窗口中播放的格式的音频,文件大小有点低。

我试过的代码如下:

 dirPaths = NSSearchPathForDirectoriesInDomains(
                                                    NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
     NSString *soundFilePath = [docsDir
                                stringByAppendingPathComponent:@"sound.wave"];
     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:8000.0], AVSampleRateKey,
                                     [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                     nil];

     NSError *error = nil;

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

     if (error)
     {
         NSLog(@"error: %@", [error localizedDescription]);

     } else {
        [audioRecorder prepareToRecord];
     }

最佳答案

我还尝试使用 AVAudioRecorder 以 AAC 编码格式录制音频,最好是在 .m4a 文件中。我很快就能够在核心音频文件 (.caf) 中获取代码以录制到 AAC,但我无法让 AVAudioRecorder 正确格式化 .m4a。我正要使用较低级别的音频单元 API 重写我的应用程序中的录音代码,但我把它放在次要位置,并添加了代码来处理共享 Audio Session 。设置完成后,我返回并尝试将其另存为 .m4a,但效果很好。

下面是一些示例代码:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                        [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                        [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                        nil];
NSError *error = nil;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
[recorder prepareToRecord];

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];

[recorder record];

然后结束我使用的录音:

[recorder stop];
AVAudioSession *session = [AVAudioSession sharedInstance];
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation;
[session setActive:NO withFlags:flags error:nil];

然后可以随意使用“tmpFileUrl”中的文件。

关于iphone - iOS:录音文件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12435683/

相关文章:

objective-c - 将自定义 View 放入 UITableView

iOS - 如何以编程方式为控件设置自定义类

ios - Arduino 和 iPhone 连接蓝牙 3.0

ios - NSURLConnection 在 iOS 6 上超时但在 iOS 5 上不超时

iphone - 如何在 iPhone 上旋转 Quartz 绘制的文字

iphone - 如何在 iOS 上构建和安装 yajl-objc

iphone - UITableView 无法从数组中获取数据

iphone - 嵌套的 UINavigationControllers

iphone - 隐藏 UIPageControl

ios - 使用 UIBezierPath 和 CoreGraphics 自由手绘和删除