diff --git a/Adafruit_VS1053.cpp b/Adafruit_VS1053.cpp index 2e8b1de..f486789 100644 --- a/Adafruit_VS1053.cpp +++ b/Adafruit_VS1053.cpp @@ -40,6 +40,7 @@ SIGNAL(TIMER0_COMPA_vect) { myself->feedBuffer(); } #endif volatile boolean feedBufferLock = false; //!< Locks feeding the buffer +boolean _loopPlayback; //!< internal variable, used to control playback looping #if defined(ESP8266) ICACHE_RAM_ATTR @@ -102,6 +103,7 @@ Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t rst, int8_t cs, playingMusic = false; _cardCS = cardcs; + _loopPlayback = false; } Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t cs, int8_t dcs, @@ -111,6 +113,7 @@ Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t cs, int8_t dcs, playingMusic = false; _cardCS = cardcs; + _loopPlayback = false; } Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t mosi, int8_t miso, @@ -122,6 +125,7 @@ Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t mosi, int8_t miso, playingMusic = false; _cardCS = cardcs; + _loopPlayback = false; } boolean Adafruit_VS1053_FilePlayer::begin(void) { @@ -174,6 +178,12 @@ boolean Adafruit_VS1053_FilePlayer::stopped(void) { return (!playingMusic && !currentTrack); } +void Adafruit_VS1053_FilePlayer::playbackLoop(boolean loopState) { + _loopPlayback = loopState; +} + +boolean Adafruit_VS1053_FilePlayer::playbackLooped() { return _loopPlayback; } + // Just checks to see if the name ends in ".mp3" boolean Adafruit_VS1053_FilePlayer::isMP3File(const char *fileName) { return (strlen(fileName) > 4) && @@ -297,10 +307,20 @@ void Adafruit_VS1053_FilePlayer::feedBuffer_noLock(void) { int bytesread = currentTrack.read(mp3buffer, VS1053_DATABUFFERLEN); if (bytesread == 0) { - // must be at the end of the file, wrap it up! - playingMusic = false; - currentTrack.close(); - break; + // must be at the end of the file + if (_loopPlayback) { + // play in loop + if (isMP3File(currentTrack.name())) { + currentTrack.seek(mp3_ID3Jumper(currentTrack)); + } else { + currentTrack.seek(0); + } + } else { + // wrap it up! + playingMusic = false; + currentTrack.close(); + break; + } } playData(mp3buffer, bytesread); diff --git a/Adafruit_VS1053.h b/Adafruit_VS1053.h index a74360f..6a45d16 100644 --- a/Adafruit_VS1053.h +++ b/Adafruit_VS1053.h @@ -391,6 +391,18 @@ class Adafruit_VS1053_FilePlayer : public Adafruit_VS1053 { * @param pause whether or not to pause playback */ void pausePlaying(boolean pause); + /*! + * @brief Set state for playback looping + * @param loopState Sets playback loop state (Note: Only use with + * startPlayingFile, if used with playFullFile no subsequent code in the + * sketch executes) + */ + void playbackLoop(boolean loopState); + /*! + * @brief Retrieve playback loop state + * @return Returns true when looped playback is enabled + */ + boolean playbackLooped(); /*! * @brief Determine current playback speed * @return Returns playback speed, i.e. 1 for 1x, 2 for 2x, 3 for 3x