简体   繁体   English

在为导航栏设置动画时使用AVAudioRecorder录制音频时发生中断时,我们如何停止并保存录制内容?

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

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. 我将记录保存到库目录下名为“ MyRecordings”的目录中的文件,如果打印该目录的内容,它将显示该文件,但是当我尝试打印持续时间时,它将给出0。

If I try to play the file at that url it gives error code 1685348671 (file invalid). 如果我尝试在该URL播放文件,它将给出错误代码1685348671(文件无效)。

How can the save the first part of the recording be saved properly? 如何正确保存录音的第一部分?

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? iPhone发生中断后如何恢复录音?

Turns out this was an issue with the timer. 原来这是计时器的问题。

The timer is called once every 2.0 / 28.0 = 0.7 seconds. 每2.0 / 28.0 = 0.7秒调用一次计时器。 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. 如果我将计时器持续时间增加到0.2,则效果很好,但动画效果不是很流畅。

With 0.1 as the timer interval it worked most of the time, but not always. 使用0.1作为计时器间隔,它在大多数时间都有效,但并非总是如此。 It was more easily reproducible on older devices (2nd/3rd generation iPod). 在较旧的设备(第2代/第3代iPod)上更容易复制。

Is it that the OS can't handle such quick updates via the timer? 是操作系统无法通过计时器处理如此快速的更新吗?

Could someone please explain what the issue might be? 有人可以解释这个问题是什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM