Skip to content

The Audio Player Class

Phil Schatzmann edited this page Aug 9, 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 (AudioSourceSdFat, AudioSourceURL, AudioSourceCallback)
  • 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

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