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