AVAudioRecorder 记录有 4 秒延迟
我正在使用 AVAudioRecorder 录制音频,但在按下按钮和开始录制之间有 4 秒的延迟。
这是我的设置代码:
NSDictionary *recordSettings = [NSDictionary 字典WithObjectsAndKeys:
[NSNumber numberWithFloat: 16000.0],AVSampleRateKey, [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil]; NSError *错误= nil; audioRecorder = [[AVAudioRecorder 分配] initWithURL:soundFileURL 设置:记录设置 错误:&错误]; 如果(错误) { NSLog(@"错误:%@", [错误本地化描述]); } 别的 { NSLog(@"准备记录"); [录音机准备录音]; }
-(无效)记录{
NSLog(@"记录"); //[audioRecorder 准备记录]; if (!audioRecorder.录音) { NSLog(@"记录2"); [录音机录音]; NSLog(@"记录3"); }
}
Record 是按下按钮时调用的函数。我知道通过“记录”隐式调用prepareToRecord,但我想看看它是否会影响延迟。事实并非如此。
这是控制台日志:
2011-10-18 21:48:06.508 [2949:707] Record
2011-10-18 21:48:06.509 [2949:707] Record 2
2011-10-18 21:48:10.047 [2949:707] Record 3
开始录制前大约有 3.5 秒。
这些设置对于iPhone来说是不是太多了? (iPhone 4)。是我初始化错误吗?
I'm using AVAudioRecorder to record audio but I'm experiencing a 4 second delay between button press and beginning to record.
Here's my setup code:
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat: 16000.0],AVSampleRateKey, [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil]; NSError *error = nil; audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error]; if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { NSLog(@"prepare to record"); [audioRecorder prepareToRecord]; }
-(void)record {
NSLog(@"Record"); //[audioRecorder prepareToRecord]; if (!audioRecorder.recording) { NSLog(@"Record 2"); [audioRecorder record]; NSLog(@"Record 3"); }
}
Record is the function called on button press. I know prepareToRecord is called implicitly via 'record' but I wanted to see if it would affect the delay at all. It does not.
Here's the console log:
2011-10-18 21:48:06.508 [2949:707] Record
2011-10-18 21:48:06.509 [2949:707] Record 2
2011-10-18 21:48:10.047 [2949:707] Record 3
There's about 3.5 seconds before it starts recording.
Are these settings too much for the iPhone? (iPhone 4). Am I initializing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 iOS5/iPhone4 组合上看到同样的问题。 iOS4和iPhone4S都可以。在旧硬件上使用新操作系统时似乎存在问题。我也尝试了很多设置组合。
尝试在初始化录音机时设置
AVAudioSession
类别:I'm seeing the same issue on the iOS5/iPhone4 combo. iOS4 and iPhone4S are fine. There seems to be an issue when using the new OS on older hardware. I've tried many combinations of settings as well.
Try setting the
AVAudioSession
category when you initialize the Audio Recorder:我在 iPod touch 第 4 代(iOS4、iOS5 和 JailBreak - 我使用了更多设备)上遇到了同样的问题,并且在不同的设备上运行不同,但我认为 @Charles Chase 是对的,它确实取决于您正在测试的设备的 iOS。您的 recordSettings 字典没问题,代码看起来也正确,除了一件事,您需要添加以下内容:
在分配并初始化记录器之后:
I had the same issue on iPod touch 4th generation (iOS4, iOS5 and JailBreak - I used more devices) and on different devices it runs different, but I think @Charles Chase is right, it does depend of device's iOS you are testing on. Your recordSettings dictionary is ok, code also looks right except one thing, you need to add this:
right after you alloc and init your recorder:
我也有同样的问题。另外,由于我在播放和录制模式之间切换,这不断导致长时间的延迟。但当我切换到播放和录音时,它拒绝使用 iPhone 上的扬声器,它只会使用耳机扬声器。
解决方案:
I had the same problem. Also, since I was switching between Playback and Record mode, this was causing long delays constantly. But when I switched to Play and Record it refused to use the loudspeaker on iPhone, it would only use the headset speaker.
Solution: