我尝试用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]];
}
}
这是我用记录器init的方法:
- (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);
}
答案 0 :(得分:1)
原因可能有很多 但就我而言, 还有另一个对象已经使用AVAudioSession来播放音频。