play wav and mp3 files from SD in the same sketch #1920
-
i have looked over and over in the examples and even here for a solution i can play a mp3 just fine using this code: and a wav file works but i cant seem to get them both to work at same time. here is my code:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 17 replies
-
I assume you are compiling in Arduino! I don't recommend to change any settings in the preferences if you don't know what you are doing. ps. your logic is completely wrong: one way to correct this is to remove the copy() from the loop and add a copier.copyAll() at the end of your play methods to process the complere file. Another is to call playwav() only after you completeley processed the first file (when the copy in the loop returns 0) |
Beta Was this translation helpful? Give feedback.
-
You defined the log level as Info in the second line in setup() ! Just change it to Warning if you don't want to see the info messages... |
Beta Was this translation helpful? Give feedback.
-
Just for inspiration. A sketch which uses the logic described in the chapter Interative Audio would look something like this: #include "SPI.h"
#include "SD.h"
#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
File file;
I2SStream i2s;
MP3DecoderHelix mp3;
WAVDecoder wav;
EncodedAudioStream decoder(&i2s, &mp3); // Decoding stream
StreamCopy copier(decoder, file);
AudioActions action;
const int chipSelect=10;
// starts playback of indicated file
void actionKeyOn(bool active, int pin, void* ptr) {
const char* fileName = (const char*)ptr;
file.close();
decoder.end();
if (StrView(fileName).endsWith("mp3")) {
decoder.setDecoder(&mp3);
}
if (StrView(fileName).endsWith("wav")) {
decoder.setDecoder(&wav);
}
decoder.begin();
file = SD.open(fileName);
}
// assign gpio buttons to playback files
void setupActions() {
action.add(6, actionKeyOn, AudioActions::ActiveLow,(void*) "file.wav");
action.add(7, actionKeyOn, AudioActions::ActiveLow,(void*) "file.mp3");
}
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial,AudioLogger::Info);
// setup SD
SD.begin(chipSelect);
// setup output (using custom I2S pins)
auto cfg = i2s.defaultConfig(TX_MODE);
i2s.begin(cfg);
setupActions();
}
// playback selected file
void loop() {
copier.copy();
action.processActions();
}
This is much shorter... |
Beta Was this translation helpful? Give feedback.
-
i love it, do you have more similar examples using:
cause im not sure i understand that section. are you simulating a button push or something? i am not seeing any way that is being called to use when you want it unless im missing it but i know this snippet
Is definitely going to come in handy lol |
Beta Was this translation helpful? Give feedback.
I assume you are compiling in Arduino!
I don't recommend to change any settings in the preferences if you don't know what you are doing.
Did you try to change the setting for compiler warnings back to Default or None ?
ps. your logic is completely wrong: one way to correct this is to remove the copy() from the loop and add a copier.copyAll() at the end of your play methods to process the complere file.
Another is to call playwav() only after you completeley processed the first file (when the copy in the loop returns 0)