Skip to content

The Audio Player Class

Phil Schatzmann edited this page Dec 10, 2022 · 37 revisions

It is pretty simple to build a simple audio player with the help of the Stream API. E.g. a SD file is a subclass of an Arduino Stream, so all you need to do is to copy from the file stream to the desired output stream. Finally you need to add some logic which handles the end of file to automatically process the next file and maybe a status flag to halt and continue the processing. In addition it adds only a little bit of additional complexity to add volume control and meta data support.

In order to simplify things, I decided to provide this functionality as well and to prove the point: The AudioPlayer class took only 120 lines of code to implement!

The AudioPlayer supports

  • multiple audio data sources (AudioSourceURL, AudioSourceCallback, File based sources (see below))
  • different Output Scenarios (I2S, PWM, A2DP etc). Just pass the desired output stream object to the constructor.
  • different Decoders for MP3, AAC, WAV. Just pass the desired decoder object to the constructor.
  • Volume Control (by calling player.setVolume())
  • Stopping and Resuming the processing (by calling player.stop() and player.play())
  • Navigation: You can move to the next file by calling player.next();
  • Metadata
  • multiple processor architectures

File Based Audio Sources

There are quite a few options to choose from.

  • AudioSourceSD: Using the Arduino SD library
  • AudioSourceIdxSD: SD Library with an index file
  • AudioSourceSDFAT: SDFAT library
  • AudioSourceIdxSDFAT: SDFAT library with index file
  • AudioSourceSDMMC: ESP32 SDMMC library
  • AudioSourceIdxSDMMC: SDMMC library with index file
  • AudioSourceSPIFFS: using the SPIFFS file system
  • AudioSourceLittleFS: using the LittleFS file system

Other Sources

If you select an index file based implementation, the system creates an index file and navigates using the content of the file. You can delete or reorder the content at your will to build your individual playlist!

Examples

Here are a couple of examples that demonstrate how to use the AudioPlayer class: