Empty file after recording voice with AVAudioRecorder

620 views Asked by At

I try to record voice with AVAudioRecorder and after recording i have a empty file with 0 byte and when try to play it, i have this warning

2016-12-31 15:28:59.511 FlowerProject[11209:4781594] 15:28:59.511
 WARNING:  [0x38cb1000] 998: Failure to setup sound, err = -50

I try to use any formats like m4a and it also 0 byte size. Thanks for any help.

EDIT:

In the same time i try to record audio and update my UI with animation

- (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]];

    }
}

And here is my method with recorder 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];

}

After recording i send sound by email using MailCore but, file is 0 byte. The recording continue 30 second and after it call method [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];
    }

}

And here is i play my sound by button click:

- (void)playSoundFromPetal:(NSString *)soundName {

    SystemSoundID mySSID;
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:soundName];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: path], &mySSID);
    AudioServicesPlaySystemSound(mySSID);

}
1

There are 1 answers

2
Goh On

There can be many reasons for that but in my case, there was another object already using AVAudioSession for playing audio.