In the last Blogs I presented
– A File Based Versatile MP3 Player
– A Streaming MP3 Player
– A Streaming AAC Player with Volume Control
All these examples were using my Arduino Audio Tools Library.
In this final Blog about this topic, I will demonstrate how easy it is to adapt the Sketch from the first Blog and send the output to a Bluetooth Speaker. Here is the Arduino Sketch which will work with a ESP32 Microcontroller:
#define USE_HELIX
#define USE_A2DP
#define USE_SDFAT
#include "AudioTools.h"
#include "AudioA2DP.h"
#include "AudioCodecs/CodecMP3Helix.h"
using namespace audio_tools;
const char *startFilePath="/";
const char* ext="mp3";
AudioSourceSdFat source(startFilePath, ext);
A2DPStream out = A2DPStream::instance(); // A2DP output - A2DPStream is a singleton!
MP3DecoderHelix decoder;
AudioPlayer player(source, out, decoder);
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
// setup output - We send the output via A2DP to a "LEXON MINO L" Bluetooth Speaker
auto cfg = out.defaultConfig(TX_MODE);
cfg.name = "LEXON MINO L";
out.begin(cfg);
// setup player
player.setVolume(0.1);
player.begin();
}
void loop() {
player.copy();
}
The only difference is that we use the A2DPStream class as output and when we configure A2DP, we need to indicate the Bluetooth Speaker name.
Finally we limit the volume and set it to 0.1.
Audio Format
The Helix decoder provides 2 channels with 16bit data usually at a sample rate of 44100 and this is exactly what is needed by A2DP!
SD Card
Here is the information how to wire the SD card to the ESP32
SD | ESP32 |
---|---|
CS | VSPI-CS0 (GPIO 05) |
SCK | VSPI-CLK (GPIO 18) |
MOSI | VSPI-MOSI (GPIO 23) |
MISO | VSPI-MISO (GPIO 19) |
VCC | VIN (5V) |
GND | GND |
Dependencies
You need to install the following libraries:
Source Code
The full (actual) source code can also be found on Github
25 Comments
Nilesh · 5. October 2024 at 6:10
PSchatzmann your work is simply awesome I am delighted , Such a inspirational documentation will guide many generations ahead even AI as well. History is/will remember this work , really I do not have word to describe.
Anonymous · 21. August 2024 at 11:43
example url is gone from github 🙏
pschatzmann · 21. August 2024 at 16:04
The A2DP examples have been moved to examples-communication/a2dp
/
Nilesh · 30. September 2024 at 19:37
Arduino: Streaming MP3 Files to a Bluetooth Speaker now various library such as AudioA2DP.h revamped or removed hence code not compiling
pschatzmann · 30. September 2024 at 19:48
This blog is several years old!
Please use the updated code that you can find on Github…
Szymon · 20. April 2024 at 13:20
Hi, is there a way to specify the duration (in seconds) of an audio file?
pschatzmann · 20. April 2024 at 15:58
Google is your friend: If that does not help I suggest to open a discussion on Github: https://github.com/pschatzmann/arduino-audio-tools/discussions
pschatzmann · 20. April 2024 at 15:58
Google is your friend: If that does not help I suggest to open a discussion on Github: https://github.com/pschatzmann/arduino-audio-tools/discussions
Frido · 2. April 2024 at 19:23
Hello,
is there already an implementation to randomly play the mp3 files from the path in a continuous loop?
pschatzmann · 2. April 2024 at 21:34
Use the FileLoop
Geoff · 18. May 2023 at 19:05
OK I figured that one out
This line:
AudioSourceSdFat source(startFilePath, ext);
should be like this:
AudioSourceSDFAT source(startFilePath, ext);
but now, it is working but my mp3 file is in fast forward, IE it plays at about 10x speed, any ideas why that is.
pschatzmann · 18. May 2023 at 19:15
A2DP espects 2 channels with 44100 samples per second. You need to make sure that you provide the result in this format!
Geoff · 18. May 2023 at 17:43
one of the includes is worng, it should be:
#include “AudioLibs/AudioA2DP.h”
Also I get error ‘AudioSourceSdFat’ does not name a type
any ideas why that is?
pschatzmann · 18. May 2023 at 19:19
Obviously a missing include: Check the documentation or use an actual example from your examples Audio Tools folder
Pitbull1969 · 28. April 2023 at 13:31
Hi , First time using Python and Arduino. I just bulit a MakePython ESP32 LCD development board by Volos projects. can I add your code to this original INO to used as bluetooth transmitter to my speakers?This MP3 uses a 3.5mm adapter. Project by volos: https://youtu.be/Paqt7YJ3TeI thanks for your help
pschatzmann · 28. April 2023 at 15:08
I don’t know all the technical details of this board, but my gut feeling is that it should be possible if it is a regular ESP32.
You just need to know and setup the pins for the SD.
I don’t understand your comment about the 3.5 Adapter: this seems to be irrelevant for this project!
pitbull1969 · 28. April 2023 at 17:50
Sorry , the 3.5 mm audio output from the audio expansion board is connected to my speaker. and that is why i want to execute the code for BT to eliminate the cable connection
my SDcard:
//SD Card
#define SD_CS 22
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
//Digital I/O used //Makerfabs Audio V2.0
#define I2S_DOUT 27
#define I2S_BCLK 26
#define I2S_LRC 25
const int Pin_vol_up = 39;
const int Pin_vol_down = 36;
const int Pin_mute = 35;
const int Pin_previous = 15;
const int Pin_pause = 33;
const int Pin_next = 2;
int visuals[6]={5,20,14,26,8,14};
int dir[5];
This board is a ESP32 Wrober/B with a colorful 1.3 inch LCD ST7789.
would i have to add the whole code ESP32 A2DP only part off it like the A Simple SdFat Audio Player i just want to send the music from my sd card to my BT speakers
Thanks for responding .I appreciate it
pschatzmann · 28. April 2023 at 18:11
Please use a discussion on github!
Ayush · 28. July 2022 at 15:48
can you please tell how to implement this on an esp32cam? for the esp32cam i need to use the sd library since the pin definitions are completely different to what has been given
pschatzmann · 28. July 2022 at 16:07
You can define your custom SPI pins with the begin method: SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
Pedro75 · 30. December 2021 at 12:05
Hello,
First of all many thanks for the great job you made with this set of libraries.
I tried to compile and run the example “SD MP3 to A2DP” it works well!
I search an example of stream MP3 from URL to A2DP in the examples folders and did not find anyone. Was my searching wrong?
If not could you give some pieces of code to do that?
Wish you a happy new year!
pschatzmann · 30. December 2021 at 12:14
https://github.com/pschatzmann/ESP32-A2DP/issues/19
Pedro75 · 30. December 2021 at 12:57
What’s a a pity !!!
By the way I have one questions regarding A2DP:
is it possible to set the BT hostname programmatically, for example:
A2DPStream out = A2DPStream::instance(“hostname”);
This will allow to have several BT sender devices with different name.
Thanks by advance for your help
Stephan · 17. April 2023 at 18:19
Hi!
Thank you for your amazing library.
As the newer board ESP32-S3 only sports BLE, I wanted to know, if one could still stream audio using an extra Bluetooth module and slightly adapting your code. Do you know of any that works?
pschatzmann · 17. April 2023 at 19:05
All the A2DP modules that I know if only support the receiving of audio.
I haven’t found anything yet which would transmit…