How to play mp3 audio stream from BLE via arduino-audio-tools? #1613
-
Problem DescriptionThank you for the wonderful library! I am using arduino-audio-tools to try to play mp3 audio streams from BLE. I am a novice, I have tried to write a lot of program code according to the case, but can not play BLE transmitted mp3 audio stream. Can you give me some help, thank you 🙏 Device DescriptionESP32 S3 + MAX98375 Sketch#include <SPI.h>
#include <TFT_eSPI.h>
#include <Wire.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BMP280.h>
#include <cmath>
#include "AudioTools.h"
#include "AudioCodecs/CodecMP3Helix.h"
#include "AudioLibs/AudioBoardStream.h"
using namespace std;
uint8_t* audio;
BLEScan* pBLEScan;
BLECharacteristic* audioCharacteristic;
BLEServer* pServer;
bool deviceConnected = false;
bool oldDeviceConnected = false;
#define SERVICE_UUID "0000A000-0000-1000-8000-00805F9B34FB"
#define writeAudioData_UUID "0000A001-0000-1000-8000-00805F9B34FB"
AudioInfo info(44100, 2, 16);
I2SStream in;
AudioBoardStream i2s(AudioKitEs8388V1);
EncodedAudioStream out(&i2s, new MP3DecoderHelix());
StreamCopy copier(out, in);
Task task("mp3-copy", 10000, 1, 0);
class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
deviceConnected = true;
};
void onDisconnect(BLEServer *pServer)
{
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
uint8_t* audio = audioCharacteristic->getData();
for (int i = 0; i < audio.size(); i++) {
Serial.print(audio[i]);
}
size_t bytes_read = in.readBytes(audio, audio.size());
}
};
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
auto cfgRx = in.defaultConfig(RX_MODE);
cfgRx.copyFrom(info);
in.begin(cfgRx);
auto config = i2s.defaultConfig(TX_MODE);
i2s.begin(config);
out.begin();
task.begin([](){copier.copy();});
BLEDevice::init("Robot");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
audioCharacteristic = pService->createCharacteristic(
writeAudioData_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
audioCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
Serial.print("Device ID:");
}
void loop() {
if (deviceConnected){
if (!deviceConnected && oldDeviceConnected)
{
delay(500);
pServer->startAdvertising();
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected)
{
oldDeviceConnected = deviceConnected;
}
} Other Steps to ReproduceHow to play unit_8* mp3 data?🙋 What is your development environmentArduino I have checked existing issues, discussions and online documentation
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 21 replies
-
This is certenly not a bug so please do not file any issue but use a discussion in the future. |
Beta Was this translation helpful? Give feedback.
-
I have encountered a similar problem. Have you made any progress? |
Beta Was this translation helpful? Give feedback.
-
I printed these out in the serial port, and it seems that the mp3 data has been decoded successfully and passed into the i2s, but I still have no sound.😭 |
Beta Was this translation helpful? Give feedback.
-
The size is wrong: you need to get it from the audioCharacteristic. |
Beta Was this translation helpful? Give feedback.
-
Call getLength() if you are using the ESP32 implementation. If I remember right the standard MTU is only 20 bytes! So I guess, you are writing 20 bytes of mp3 data and the rest is garbage with each call |
Beta Was this translation helpful? Give feedback.
I suggest to have a look at the examples