Help with ESP32 bluetooth coding

I need Help with Coding .I am trying to make bluetooth speaker using esp32 . i playing audio using external DAC now what i want is .When i connect audio i play a tone stored in code .to play the sound .but when i play audio through my phone to esp32 then i cant hear the connect and disconnect sound .The audio log is showing fine that it is playing but no sound is heard .it fixes when i reset the board .once any music played through speaker then it is done
no sound for disconect and connect .

#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
#include "BluetoothA2DPSink.h"
#include "tone.h"  // contains zero_mp3 and zero_mp3_len

using namespace audio_tools;

I2SStream i2s;
MP3DecoderHelix helix;
EncodedAudioStream out(&i2s, &helix);
BluetoothA2DPSink a2dp_sink(i2s);
StreamCopy* copier;  // Global copier
MemoryStream* mp3;   // Global MemoryStream

bool wasConnected = false;

void setup() {
  Serial.begin(115200);
  AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);

  // I2S configuration
  auto cfg = i2s.defaultConfig();
  cfg.sample_rate = 44100;  // Match with MP3 sample rate
  cfg.channels = 1;
  cfg.pin_bck = 27;
  cfg.pin_ws = 26;
  cfg.pin_data = 25;
  i2s.begin(cfg);
  out.begin();

  // Start Bluetooth
  a2dp_sink.start("MyMusic");
}

// MP3 tone playback
void playTone(bool connect) {

  if (connect) {
    mp3 = new MemoryStream((const uint8_t*)discord_connect, discord_connect_len);
  } else {
    a2dp_sink.stop();
    delay(100);
    mp3 = new MemoryStream((const uint8_t*)discord_disconnect, discord_disconnect_len);
  }

  copier = new StreamCopy(out, *mp3);

  // Start the decoder and play the tone
  helix.begin();
  copier->copyAll();
  helix.end();

  // Clean up the MemoryStream and StreamCopy
  delete mp3;
  delete copier;
}



void loop() {
  bool currentlyConnected = a2dp_sink.is_connected();

  if (currentlyConnected && !wasConnected) {
    Serial.println("Bluetooth Connected");
    wasConnected = true;
    playTone(wasConnected);  // 🔊 Play on connect
  } else if (!currentlyConnected && wasConnected) {
    Serial.println("Bluetooth Disconnected");
    wasConnected = false;
    playTone(wasConnected);
  }
}

This is the code is use for now tone.h contains audio array files of tone

Can you help me find out the issue

The issue was caused by how the Bluetooth A2DP stack and the I2S peripheral were being managed and reused. Once Bluetooth audio was streamed via A2DP, it seemed to take control of the I2S interface and modify its configuration. After this point, when trying to play connect or disconnect tones using the same I2S stream, the playback would fail unless the device was restarted. This happened because the I2S configuration remained in a state incompatible with the tone playback method. I resolved the issue by explicitly stopping the A2DP stream and reinitializing the I2S configuration before playing the tones. This ensured that the I2S peripheral was reset to a clean and expected state, allowing the MP3 tones to play correctly even after Bluetooth audio had been used.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.