gpt4 book ai didi

iphone - iOS:录音文件格式

转载 作者:技术小花猫 更新时间:2023-10-29 10:21:40 28 4
gpt4 key购买 nike

我正在实现录音。它适用于 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com