使用 AVAudioRecorder 录制语音后的空文件
Posted
技术标签:
【中文标题】使用 AVAudioRecorder 录制语音后的空文件【英文标题】:Empty file after recording voice with AVAudioRecorder 【发布时间】:2016-12-31 09:48:33 【问题描述】:我尝试使用 AVAudioRecorder 录制语音,录制后我有一个 0 字节的空文件,当尝试播放它时,我有这个警告
2016-12-31 15:28:59.511 FlowerProject[11209:4781594] 15:28:59.511
WARNING: [0x38cb1000] 998: Failure to setup sound, err = -50
我尝试使用任何格式,例如 m4a,它也是 0 字节大小。感谢您的帮助。
编辑:
同时我尝试录制音频并用动画更新我的 UI
- (void)commanClickOnPetal:(FlowerObject *)flowerObject andButton:(UIButton *)sender
if ([[NSUserDefaults standardUserDefaults] boolForKey:flowerObject.imageName] == NO || ![[NSUserDefaults standardUserDefaults] boolForKey:flowerObject.imageName])
[self.flowerObject changeRecordedState:YES andKey:flowerObject.imageName];
self.view.userInteractionEnabled = NO;
[self playFirstSound];
[self initRecorderWithName:[NSString stringWithFormat:@"%@sound", flowerObject.imageName]];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
[self startRecording];
[self.recordIdentifierLabel setHidden:NO];
[UIView animateWithDuration:1.f
delay:0.f
options: UIViewAnimationOptionRepeat | UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse
animations:^
[self.recordIdentifierLabel setAlpha:0.0f];
completion:nil];
[UIView transitionWithView:sender duration:29.0f options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:^
[sender setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@BW",flowerObject.imageName]] forState:UIControlStateNormal];
completion:^(BOOL finished)
self.view.userInteractionEnabled = YES;
[self.recordIdentifierLabel setHidden:YES];
];
);
else
[self playSoundFromPetal:[NSString stringWithFormat:@"%@sound", flowerObject.imageName]];
这是我的记录器初始化方法:
- (void)initRecorderWithName:(NSString *)name
self.recordName = [NSString stringWithFormat:@"%@.wav",name];
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
self.recordName, nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
self.currentSession = [AVAudiosession sharedInstance];
[self.currentSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSMutableDictionary *recordingSettings= [[NSMutableDictionary alloc] init];
[recordingSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordingSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordingSettings setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordingSettings setValue:[NSNumber numberWithInt: 16] forKey:AVLinearPCMBitDepthKey];
[recordingSettings setValue:[NSNumber numberWithBool: NO] forKey:AVLinearPCMIsBigEndianKey];
[recordingSettings setValue:[NSNumber numberWithBool: NO] forKey:AVLinearPCMIsFloatKey];
[recordingSettings setValue:[NSNumber numberWithInt: AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordingSettings error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
录制后,我使用 MailCore 通过电子邮件发送声音,但文件为 0 字节。录制持续 30 秒后调用方法 [self stopRecording]
- (void) startRecording
if (!recorder.recording)
AVAudioSession *session = self.currentSession;
[session setActive:YES error:nil];
[recorder record];
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
else
[recorder stop];
- (void) stopRecording
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
self.dictionaryWithRecords = (NSMutableDictionary *) @@"URL": recorder.url,
@"Name": self.recordName;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
if ([[DBEmailSender sharedManager] emailAddress] != nil)
[[DBEmailSender sharedManager] sendMailWithAddress:[[NSUserDefaults standardUserDefaults] objectForKey:@"email"] andCode:nil andFilePath:[self.dictionaryWithRecords objectForKey:@"Name"]];
);
- (void)updateCountdown
self.tick += 1;
double seconds = fmod(self.tick, 60.0);
if (seconds == 29)
self.tick = 0;
seconds = 0;
[self.timer invalidate];
self.timer = nil;
[self stopRecording];
这是我通过点击按钮播放我的声音:
- (void)playSoundFromPetal:(NSString *)soundName
SystemSoundID mySSID;
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:soundName];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: path], &mySSID);
AudioServicesPlaySystemSound(mySSID);
【问题讨论】:
【参考方案1】:这可能有很多原因 但就我而言, 已经有另一个对象使用 AVAudioSession 播放音频。
【讨论】:
那么您是如何从该对象中获取音频会话的? @hyd00 好吧,我实际上意识到另一个对象的会话并不是真正必要的,所以我只是删除了它。但是你也可以试试: let sharedSession = AVAudioSession.sharedInstance() do try sharedSession.setCategory(AVAudioSessionCategoryRecord) try sharedSession.setActive(true) catch以上是关于使用 AVAudioRecorder 录制语音后的空文件的主要内容,如果未能解决你的问题,请参考以下文章