Adafruit Music Maker, a simple player

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Zlshep
 
Posts: 16
Joined: Wed Aug 04, 2021 12:02 pm

Adafruit Music Maker, a simple player

Post by Zlshep »

Hi all, I am struggling to understand how to use the Music Maker. I don't think I am very good at coding tbh. All I want is, when it is powered on, for my Uno with the Music Maker shield, to play music tracks, one after another, and then loop to the beginning after playing all tracks. I think that is fairly easy to do with files like track001.mp3 and then add 1 to the string and make it track002.mp3 etc. But I also want to add push buttons for pause, skip to next song and skip to previous song. I also have an LCD display that I would like to display the track name on, but I think I am running out of pins! Any guidance would be really appreciated!

User avatar
Zlshep
 
Posts: 16
Joined: Wed Aug 04, 2021 12:02 pm

Re: Adafruit Music Maker, a simple player

Post by Zlshep »

Hi all so I have solved this, I want to document how for any future readers.
The code is essentially as follows:

Code: Select all

void loop() {
char c = Serial.read();//read from the console, used to give play/pause, skip track etc
if (musicPlayer.stopped() {
   //code here to select the next file. I have code that says tracknum = tracknum + 1
   musicPlayer.startPlayingFile(tracknum);
   }
//
//
// Here is where you add your if statements for pausing, playing etc.
    if (c == 's') {//input to play/pause here in the if statement
    if (! musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true); //this code is to identify whether the player is paused or not. If it
      } else {                                             //is paused, it will play. If it is playing, it will pause.
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
If you are going to go to previous track, with my above code, you'll need to subtract 2 from tracknum, because then when it goes back to the top of void loop() it will add 1 again. You'll also need to use musicPlayer.stopPlaying(); in order for the code above to play the next file. When skipping forward a track, you can just do musicPlayer.stopPlaying(); as the code at the top will automatically add 1 and move to the next track when play is stopped.

I hope this helps someone build a basic music player with play/pause, next and previous track! I have also added volume controls to mine, by using an int variable "vol" when using musicPlayer.setVolume(vol, vol); this way I can add or subtract numbers from vol and decrease or increase the volume, respectively. DON'T FORGET THAT BIGGER NUMBERS MEAN QUIETER, AND SMALLER NUMBERS MEAN LOUDER!!!

MassimoManca
 
Posts: 10
Joined: Mon May 27, 2019 9:32 am

Re: Adafruit Music Maker, a simple player

Post by MassimoManca »

I do not like so much how set volume works (0...0xfe=254) where 0 is the highest volume and 254 the lowest so I suggest to use something as the map function (Arduino library) to have better management:

Adafruit_VS1053_FilePlayer mp3 = Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);

uint8_t vol = 75: // 0 <= vol <= 100
uint8_t volume = (uint8_t)map(vol, 0, 100, 254, 0);
mp3.setVolume(volume, volume);

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”