reciving and playing audio files through esp 32
reciving and playing audio files through esp 32
deleting them
We can transfer audio files over Bluetooth using Bluetooth Serial (SPP) or Bluetooth Low
Energy (BLE) and store them in SPIFFS, LittleFS
How It Works:
3. Later, ESP32 can play the stored file using an I2S DAC modul
Once the audio file is stored, you have two ways to play it:
ESP32 has an I2S (Inter-IC Sound) interface that works with DAC modules like MAX98357A.
The stored file (MP3/WAV) is read and played via a connected speaker.
1 NESSASARY PROGRAM REQUIRED
This method receives an audio file over Bluetooth, stores it, and plays it through an I2S speaker.
#include "BluetoothSerial.h"
#include "SPIFFS.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32-Audio-Receiver");
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed");
return;
}
}
void loop() {
if (SerialBT.available()) {
File file = SPIFFS.open("/audio.mp3", FILE_WRITE);
if (!file) {
Serial.println("Failed to open file");
return;
}
while (SerialBT.available()) {
file.write(SerialBT.read());
}
file.close();
Serial.println("File saved!");
}
}
2. Play Stored Audio File via I2S
#include "Arduino.h"
#include "Audio.h"
Audio audio;
void setup() {
Serial.begin(115200);
audio.setPinout(26, 25, 22); // I2S connections
audio.begin("/audio.mp3", "SPIFFS");
}
void loop() {
audio.loop();
}
audio files are stored in SPIFFS or LittleFS, We can delete them using the remove() function.
Required program
#include "SPIFFS.h"
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed");
return;
}
if (SPIFFS.remove("/audio.mp3")) {
Serial.println("File deleted successfully");
} else {
Serial.println("File deletion failed");
}
}
void loop() {
}