popping issues while stopping and starting playback and when switching streams #715
-
hi, #include "AudioTools.h"
#include "BluetoothA2DPSink.h"
#include "SPIFFS.h"
AnalogAudioStream analogOut;
NullStream noth;
BluetoothA2DPOutputAudioTools btOutput;
BluetoothA2DPSink a2dp_sink(btOutput);
EncodedAudioStream decoder(&analogOut, new WAVDecoder());
WAVDecoder wavDec;
void playEventSound(const String &filename)
{
AudioInfo c_info = analogOut.audioInfo();
btOutput.set_output(noth);
decoder.begin();
File f = SPIFFS.open(filename);
StreamCopy copier(decoder, f);
copier.copyAll();
f.close();
decoder.end();
analogOut.setAudioInfo(c_info);
btOutput.set_output(analogOut);
}
void on_connection_state_changed(esp_a2d_connection_state_t state, void *ptr)
{
if (state == ESP_A2D_CONNECTION_STATE_CONNECTED)
{
playEventSound("/connect.wav");
}
else if (state == ESP_A2D_CONNECTION_STATE_DISCONNECTED)
{
playEventSound("/disconnect.wav");
}
}
void setup()
{
SPIFFS.begin();
auto cfg = analogOut.defaultConfig();
analogOut.begin(cfg);
a2dp_sink.set_on_connection_state_changed(on_connection_state_changed);
a2dp_sink.set_auto_reconnect(true, 10);
a2dp_sink.set_volume(150);
a2dp_sink.set_mono_downmix(true);
playEventSound("/poweron.wav");
a2dp_sink.start("esp32");
}
void loop()
{
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
Did you try my proposed solution with the A2DPStream ? So I suggest you try the easy solution first before digging into the complicated ones... |
Beta Was this translation helpful? Give feedback.
-
Just look at the examples that start with streams-a2dp and replace the audio sink |
Beta Was this translation helpful? Give feedback.
-
I would expect that your forcing the channel to 1 might be problematic. #include "AudioTools.h"
#include "SPIFFS.h"
#include "AudioTools/AudioLibs/A2DPStream.h"
AnalogAudioStream out;
A2DPStream a2dpStream;
StreamCopy copier(out, a2dpStream);
// for esp_a2d_connection_state_t see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_a2dp.html#_CPPv426esp_a2d_connection_state_t
void connection_state_changed(esp_a2d_connection_state_t state, void *ptr) {
switch (state) {
case ESP_A2D_CONNECTION_STATE_CONNECTED:
playEventSound("/connect.wav");
break;
case ESP_A2D_CONNECTION_STATE_DISCONNECTED:
playEventSound("/disconnect.wav");
break;
}
}
void playEventSound(const String &filename) {
File f = SPIFFS.open(filename, "r");
if (!f) return;
// play file
WAVDecoder wav;
EncodedAudioStream decoder(&out, &wav);
decoder.begin();
StreamCopy stat_copy(decoder, f);
stat_copy.copyAll();
// restore audio info from a2dp
out.setAudioInfo(a2dpStream.audioInfo());
}
void setup() {
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
SPIFFS.begin();
auto cfg = out.defaultConfig(TX_MODE);
out.begin(cfg);
playEventSound("/poweron.wav");
auto btconf = a2dpStream.defaultConfig(RX_MODE);
btconf.auto_reconnect = true;
btconf.name = "esp32 player";
// btconf.silence_on_nodata = true; // I don't think this is needed
a2dpStream.begin(btconf);
// register connection state callback
a2dpStream.sink().set_on_connection_state_changed(connection_state_changed);
// wait for connection
while (!a2dpStream.isConnected())
delay(100);
}
void loop() {
copier.copy();
} |
Beta Was this translation helpful? Give feedback.
Did you try my proposed solution with the A2DPStream ? So I suggest you try the easy solution first before digging into the complicated ones...