I want to play multiple audio files from an SD card with an ESP32.
I am using esp32-i2s-sd-player library, it works as is below with one file but I cant work out how to play multiple files one after another.
Can anyone put me on the correct path to do this please?
Thanks
/*
ESP32 SD I2S Music Player
esp32-i2s-sd-player.ino
Plays MP3 file from microSD card
Uses MAX98357 I2S Amplifier Module
Uses ESP32-audioI2S Library - https://github.com/schreibfaul1/ESP32-audioI2S
*
DroneBot Workshop 2022
https://dronebotworkshop.com
*/
// Include required libraries
#include "Arduino.h"
#include "Audio.h"
#include "SD.h"
#include "FS.h"
// microSD Card Reader connections
#define SD_CS 5
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
// I2S Connections
#define I2S_DOUT 26 // 22
#define I2S_BCLK 27 // 26
#define I2S_LRC 25
// Create Audio object
Audio audio;
void setup() {
// Set microSD Card CS as OUTPUT and set HIGH
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Initialize SPI bus for microSD Card
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
// Start Serial Port
Serial.begin(115200);
// Start microSD Card
if(!SD.begin(SD_CS))
{
Serial.println("Error accessing microSD card!");
while(true);
}
// Setup I2S
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
// Set Volume
audio.setVolume(15);
// Open music file
audio.connecttoFS(SD,"/charging.mp3");
}
void loop()
{
audio.loop();
}
I mean, I want to call different audio files from the main loop, to start with just several audio files cycling but later with logic tests of a push button, all it says is the following.
How do I code actual different audio files as that seems to be hard coded in setup which is one file only.
I have tried that one before and it gives me compile errors...
They seem to just use the audio.connecttoFS(SD,"/xxx.mp3"); command but when i try that i get silence?
/*
ESP32 SD I2S Music Player
esp32-i2s-sd-player.ino
Plays MP3 file from microSD card
Uses MAX98357 I2S Amplifier Module
Uses ESP32-audioI2S Library - https://github.com/schreibfaul1/ESP32-audioI2S
*
DroneBot Workshop 2022
https://dronebotworkshop.com
*/
// Include required libraries
#include "Arduino.h"
#include "Audio.h"
#include "SD.h"
#include "FS.h"
// microSD Card Reader connections
#define SD_CS 5
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
// I2S Connections
#define I2S_DOUT 26 // 22
#define I2S_BCLK 27 // 26
#define I2S_LRC 25
// Create Audio object
Audio audio;
void setup() {
// Set microSD Card CS as OUTPUT and set HIGH
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Initialize SPI bus for microSD Card
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
// Start Serial Port
Serial.begin(115200);
// Start microSD Card
if(!SD.begin(SD_CS))
{
Serial.println("Error accessing microSD card!");
while(true);
}
// Setup I2S
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
// Set Volume
audio.setVolume(15);
// Open music file
// audio.connecttoFS(SD,"/charging.mp3");
}
void loop()
{
//audio.loop();
audio.connecttoFS(SD,"/charging.mp3");
delay (5000);
audio.connecttoFS(SD,"/fire-full_power.mp3");
delay (5000);
}
What is the best way to play two audio clips triggered by button 1 & button 2?
I know I need to use an SD card as not enough mem to use encoded method.
Ideally using an ESP32, I already tried the ESP32-audioI2S Library but cant work out how to do it, previous support posts havent helped.
Are there any other methods?
Seems like something which shouldnt be this hard.
All the single file examples play it in setup()
This is where I got stuck.
When I try to play from main loop I just cant get anything to work....
Here is the single file example that works
//**********************************************************************************************************
//* audioI2S-- I2S audiodecoder for ESP32, *
//**********************************************************************************************************
//
// first release on 11/2018
// Version 3 , Jul.02/2020
//
//
// THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT.
// FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR
// OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//
#include "Arduino.h"
#include "WiFiMulti.h"
#include "Audio.h"
#include "SPI.h"
#include "SD.h"
#include "FS.h"
// Digital I/O used
#define SD_CS 5
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
#define I2S_DOUT 26
#define I2S_BCLK 27
#define I2S_LRC 25
Audio audio;
void setup() {
pinMode(SD_CS, OUTPUT); digitalWrite(SD_CS, HIGH);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
SPI.setFrequency(1000000);
Serial.begin(115200);
SD.begin(SD_CS);
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(15); // 0...21
audio.connecttoFS(SD, "/test1.mp3");
}
void loop()
{
audio.loop();
}
But when I place the audio.connecttoFS(SD, "/test1.mp3"); in the main loop just as a test I get nothing...
I am pulling my hair out, I cant see why the code below does not work, i just get silence...
I just want to play two audio files one after another.
I know wiring is correct as single audio files can be played when audio.connecttoFS(SD, "charging.mp3"); is placed in setup but won't work when called from main loop.
I have tried many demo's & sample code and none of them work for me but all seem to do it as I have coded.
You need to put filenames in an array.
Then put audio.loop(); in loop.
It needs to be continuously, so you will need non blocking code.
In the loop you can set a timer or you can test for end of audio file or you can read some button. Depending on that, you can set another audio file from your array. Read about non blocking code...
The timer cannot use delay! That is blocking code.... Timer might be unhandy as songs will have different length. I guess your library has some test for end of audio file. Maybe audio.loop returns false at end of file?