Audio Data writing , stuck in infinite loop

I have an amb82 mini , I am trying to write audio data from api response to an audio file using wificlient library. The logic works as follows:
Opens an audio file named as output.mp3 using open function.
Read a single audio binary byte from the server response.
Write this byte to audio file
Do the above till we reach the last byte of audio data.

This is my code:

fs.begin();
  File Afile = fs.open("/output.mp3");
if (!Afile) {
  Serial.println("Error opening file for writing");
  return;
}
else {
  Serial.println("Opened Audio File Successfully");
}

while (client.connected()) {
  Serial.println("I am inside While Loop");
  int bytesAvailable = client.available();
  Serial.println(bytesAvailable);
  
  if (bytesAvailable > 0) {
    Serial.println("I am inside client.avail condition");
    char c = client.read();
    Afile.write(c);
    Serial.println("I have written one byte in audio file");
  }
  else {
    client.stop();
    Serial.println("I have stopped client");
    break; // Exit the loop if no more data available
  }
}

Serial.println("I am outside Loop, success");
Afile.close();
Serial.println("File saved ");

    listFiles("/");
    
    fs.end();

I am getting this in response:
13:24:53.546 → I am inside client.avail condition

13:24:53.546 → I have written one byte in audio file

13:24:53.546 → I am inside While Loop

13:24:53.546 → 1

13:24:53.546 → I am inside client.avail condition

13:24:53.546 → I have written one byte in audio file

13:24:53.546 → I am inside While Loop

13:24:53.546 → 1

13:24:53.546 → I am inside client.avail condition

13:24:53.546 → I have written one byte in audio file

13:24:53.546 → I am inside While Loop

13:24:53.546 → 1

13:24:53.546 → I am inside client.avail condition

13:24:53.546 → I have written one byte in audio file

13:24:53.576 → I am inside While Loop

13:24:53.576 → 1

13:24:53.576 → I am inside client.avail condition

13:24:53.576 → I have written one byte in audio file

13:24:53.576 → I am inside While Loop

13:24:53.576 → 1

13:24:53.576 → I am inside client.avail condition

13:24:53.576 → I have written one byte in audio file

13:24:53.576 → I am inside While Loop

13:24:53.576 → 1

13:24:53.576 → I am inside client.avail condition

13:24:53.576 → I have written one byte in audio file

13:24:53.576 → I am inside While Loop

It is infinte loop, because of some condition problem , I want to know that how many audio bytes are still available, because the client.available is only giving me 1 as response it is not giving me the number of audio bytes, or how can i know that the audio data has ended so that i can update condition accordingly.
please help me with that.

Hi @HASSAN_ALI ,

From your server, after your audio sending has stopped, you can send another unique character via wificlient to indicate it is the end of audio file. Since it is constantly reading the char c, once you receive that unique character, you can stop writing into the audio file. Make sure not to write the unique character into your file.
Thank you.

If there is time for us to look into the client.available() API, we will fix it. But for now, we will leave it as it is first, until we confirm the fix.