How do we stop and save a recording when an interruption occurs while recording audio using AVAudioRecorder while animating the navigation bar?

StackOverflow https://stackoverflow.com/questions/17673448

  •  03-06-2022
  •  | 
  •  

Question

I'm animating the navigation bar using a timer when recording starts and stopping it when the recording stops (to make it similar to the Voice Memos app).

Here is the code

//do nav bar animation
_navigationImageCount = 0;
self.navigationBarTimer = [NSTimer scheduledTimerWithTimeInterval:(2.0 / 28.0) target:self selector:@selector(changeNavigationBarImage:) userInfo:nil repeats:YES];

- (void)changeNavigationBarImage:(NSTimer *)timer
{   
    NSMutableArray *navigationBarImages = [NSMutableArray arrayWithArray:@[@"nav_bar_01.png", @"nav_bar_02.png", @"nav_bar_03.png", @"nav_bar_04.png", @"nav_bar_05.png", @"nav_bar_06.png", @"nav_bar_07.png", @"nav_bar_08.png", @"nav_bar_09.png", @"nav_bar_10.png", @"nav_bar_11.png", @"nav_bar_12.png", @"nav_bar_13.png", @"nav_bar_14.png"]];
    [navigationBarImages addObjectsFromArray:[[navigationBarImages reverseObjectEnumerator] allObjects]];

    NSInteger imageIndex = _navigationImageCount % 28;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:navigationBarImages[imageIndex]] forBarMetrics:UIBarMetricsDefault];

    _navigationImageCount++;
}


- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder
{
    [recorder stop];
    [self.recordingTimer invalidate]; //timer used to update the UI
    [self.navigationBarTimer invalidate];
}

I am saving the recording to a file to a directory named "MyRecordings" under the Library directory and if print the contents of that directory it shows the file, however when I try to print the duration it gives 0.

If I try to play the file at that url it gives error code 1685348671 (file invalid).

How can the save the first part of the recording be saved properly?

Was it helpful?

Solution 2

Turns out this was an issue with the timer.

The timer is called once every 2.0 / 28.0 = 0.7 seconds. That is what was causing the issue. If I increased the timer duration to 0.2 it worked fine but the animation wasn't very smooth.

With 0.1 as the timer interval it worked most of the time, but not always. It was more easily reproducible on older devices (2nd/3rd generation iPod).

Is it that the OS can't handle such quick updates via the timer?

Could someone please explain what the issue might be?

OTHER TIPS

Hope this helps.

Problem is solved in the thread. You need to pause & resume your audio queue. I have worked on this one year back, I don't remember the exact code but you will idea from here.

how to resume recording after interruption occured in iphone?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top