Continue recording if sound is present

Good day!
I am creating a small project - voice activated voice recorder. The project is based on this library: GitHub - TMRh20/TMRpcm: Arduino library for asynchronous playback of PCM/WAV files direct from SD card. Arduino Uno,Nano,Mega etc supported
I wrote a code in which the recording starts automatically if the sound level exceeds a certain value. Recording stops after a specified period of time. I try to make the recording not stop, but continue if there is sound. Here is the code:

#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#include <EEPROM.h>

#define SD_ChipSelectPin 10  //

#define ledStart 2         /* PIN LED Start */
#define ledStop 3          /* PIN LED Stop */
#define MicPin A1         
//#define ReadPin A2         

char NameRecord[10];    // Имя нового - записываемого файла на SD-карту
int RecordNumber;       // Номер записи - храним в EEPROM. в диапазоне от 0 до 32767
byte Recording = 0;     // Переменная определяет идет сейчас запись или нет
int RecInterval = 5;    // Минимальная продолжительность записи в секундах
unsigned long TimeInterval = 0; // Переменная для вычисления времени
int MaxAnalogPinValue = 1020;   // Уровень сигнала на аналоговом входе при котором произойдет старт записи
int SaveADC1 = 0;
int SaveADC2 = 0;

                                //

TMRpcm audio;   // create an object for use in this sketch 

void setup() {
    Serial.begin(9600);
    
    pinMode(ledStart, OUTPUT);
    pinMode(ledStop, OUTPUT);
    //pinMode(14, OUTPUT); //A0
    //pinMode(16, OUTPUT); //A2
    //digitalWrite(14, LOW);
    //digitalWrite(16, LOW);
    audio.speakerPin = 11; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
    pinMode(10,OUTPUT);  //Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
    
    if (!SD.begin(SD_ChipSelectPin)) {  
        digitalWrite(ledStop, LOW);
        digitalWrite(ledStart, LOW);
        return;
    }else{
        digitalWrite(ledStop, LOW);
        digitalWrite(ledStart, HIGH);
    }
    analogReference(EXTERNAL);
    // The audio library needs to know which CS pin to use for recording
    audio.CSPin = SD_ChipSelectPin;
    
    RecordNumber = EEPROM.read(0);
    RecInterval = RecInterval * 1000;
}


void loop() {
    if (Recording == 0){
        int AnR = analogRead(MicPin);
        Serial.println(AnR);
        if (AnR >= MaxAnalogPinValue) {

            Serial.println(1);
            StartRec();
        }
    }
    
    if (Recording == 1 && millis() - TimeInterval >= RecInterval) {
        int TempSaveA = ADCSRA;
        int TempSaveB = ADCSRB;
        ADCSRA = SaveADC1;
        ADCSRB = SaveADC2;
        int AnR = analogRead(MicPin);
        Serial.println(AnR);
        if (AnR < MaxAnalogPinValue) {
            ADCSRA = TempSaveA;
            ADCSRB = TempSaveB;
            StopRec();
            Serial.println(2);
        }else{
            TimeInterval = millis()-1;
            Serial.println(3);
        }
        ADCSRA = TempSaveA;
        ADCSRB = TempSaveB;
    }
}

void StartRec() {                   // begin recording process
    SaveADC1 = ADCSRA;
    SaveADC2 = ADCSRB;
    Recording = 1;
    digitalWrite(ledStop, HIGH);
    digitalWrite(ledStart, LOW);
    RecordNumber++;
    if (RecordNumber > 32766)RecordNumber = 0;      // небольшое огриничение в номерации файлов
    EEPROM.write(0, RecordNumber);                  // Сохранение в EEPROM номера последнего аудиофайла
    TimeInterval = millis();           // Запоминаем  millis для отсчета времени записи
    sprintf(NameRecord,"%d.wav", RecordNumber);     // создаем название файла из номера и расширения ".wav"
    audio.startRecording(NameRecord, 16000, MicPin, 0);// Старт записи
}

void StopRec() {                    // stop recording process, close file
    audio.stopRecording(NameRecord);// Стоп записи
    digitalWrite(ledStop, LOW);
    digitalWrite(ledStart, HIGH);
    Recording = 0;
    ADCSRA = SaveADC1;
    ADCSRB = SaveADC2;
}

But the recording still stops, and starts again, instead of continuing without stopping if there is sound.
Can you tell me how to fix this?
Sorry for my english.

I bring a complete sketch. Recording is activated by voice. If there is sound, then the recording is not turned off. Each new recording is saved with a new file name.
The circuit diagram of the device was created in the Fritzing program: Audio Recorder Schematics.fzz - Google Drive
Sketch: sketch_dec15a.ino - Google Drive