0

I have an MP3 shield on a Arduino Uno. When using "File Player" I can get the tracks to play by keyboard entry with three digits (001-009).

The problem is that I want to use (SFEMP3Shield > examples > MP3ButtonPlayer2) that is looking for files 1-9. Any file numbers other than 1,2,3,4,5,6,7,8,9 doesn’t work. Any suggestions?

Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56

1 Answers1

1

The playTrack function used in MP3ButtonPlayer2 example plays file with name created with printf format "track%03d.mp3". %03d creates a number prefixed with zeros to lenght 3. For example track005.mp3 or track042.mp3 or track999.mp3.

implementation of the playTrack function:

uint8_t SFEMP3Shield::playTrack(uint8_t trackNo){

  //a storage place for track names
  char trackName[] = "track001.mp3";
  uint8_t trackNumber = 1;

  //tack the number onto the rest of the filename
  sprintf(trackName, "track%03d.mp3", trackNo);

  //play the file
  return playMP3(trackName);
}

change it to:

uint8_t SFEMP3Shield::playTrack(uint8_t trackNo){

  //a storage place for track names
  char trackName[13];
  int trackNumber = trackNo;

  //tack the number onto the rest of the filename
  sprintf(trackName, "track%03d.mp3", trackNumber);

  //play the file
  return playMP3(trackName);
}
Juraj
  • 18,037
  • 4
  • 29
  • 49
  • Thank you, makes sense, but I'm not a programmer, I don't know how to edit the SFEMP3Shield > examples > MP3ButtonPlayer2 to make it work. – Darryl Green Oct 22 '18 at 17:59
  • 1
    put the SD card in a computer and rename the files. You should buy an MP3 player, not an Arduino, if you don't want to code – Juraj Oct 22 '18 at 18:10
  • It's part of a larger program that uses code that I have worked out. This is the only thing I haven't been able to work, although Sparkfun says it should out of the box. It just need to play 3 tracks when things happen. I have renamed the tracks may times. Does the code you gave go on the SD card or into the MP3ButtonPlayer2 code? – Darryl Green Oct 22 '18 at 20:02
  • it doesn't go anywhere. it is in the library. (and it's a shame for the author of the library, because it should not be coded as it is) – Juraj Oct 22 '18 at 20:06
  • The SFEMP3Shield library is included in the sketch. What am I supposed to rename the tracks to? When I run File Player I get; – Darryl Green Oct 22 '18 at 20:23
  • Received command: l Music Files found : 00001: SndoSil.mp3 00002: Stmup.mp3 00003: Blndblt.mp3 I have renamed the files 001,002,003 with no change. – Darryl Green Oct 22 '18 at 20:24
  • The 5 digit number is not on the SD card. It is generated in file player. – Darryl Green Oct 22 '18 at 20:25
  • it should be track001.mp3 – Juraj Oct 23 '18 at 05:11
  • It’s not, that’s my point.I have renamed it track001.mp3 and somewhere in the upload it appears as track00001.mp3. Sparkfun tech support told me to input two leading zeros and since I proved the shied works they're done and told me to figure it out myself. Do you know how to add two leading zeros @ int8_t track = 0; in the MP3ButtonPlayer2 sketch? – Darryl Green Oct 27 '18 at 15:49
  • C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp: In member function 'uint8_t SFEMP3Shield::vs_init()':

    C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp:292:34: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    if(VSLoadUserCode("patches.053")) return 6;

    – Darryl Green Oct 27 '18 at 15:51
  • function playTrack will add two leading zeros, if you have the same library. call it as playTrack(1), playTrack(2), playTrack(3), – Juraj Oct 27 '18 at 15:52
  • It needs to add two "ADDITIONAL" zeros. The library is SFEMP3Shield and I have downloaded it from many places and having different dates. here is what I get from "FilePlayer" Received command: l Music Files found : 00001: SndoSil.mp3 00002: Stmup.mp3 00003: Blndblt.mp3 00004: track001.mp3 Sparkfun told me that they noticed it was adding additional zeros and to input two additional zeros via keyboard in "FilePlayer" and it would work which it does but I want to use MP3ButtonPlayer2 which generates it's own number and doesn't work. – Darryl Green Oct 27 '18 at 16:13
  • The MP3ButtonPlayer2 sketch does call for playTrack(1), playTrack(2), playTrack(3) it should cal for playTrack(001), playTrack(002), playTrack(003) or the SFEMP3Shield library need to be fixed. – Darryl Green Oct 27 '18 at 16:16
  • the parameter of function playTrack is int. the function builds the name as you see in my answer. read the answer again – Juraj Oct 27 '18 at 16:17
  • I've read your answer several times.I understand the sketch does add two zeros. it needs to add two MORE zeros or the SFEMP3Shield library needs to be fixed. – Darryl Green Oct 27 '18 at 16:55
  • but SD card supports only 8 characters long names (plus file extension) – Juraj Oct 27 '18 at 17:00
  • from where do you have the library? – Juraj Oct 27 '18 at 17:01
  • The names are 8 characters or less. The five digit number shouldn't be there. I didn't put it there. Like I said, I have downloaded from several places older & newer versions all with the same result. Could you send me a link to where you believe I can get a right one? – Darryl Green Oct 27 '18 at 17:34
  • https://github.com/madsci1016/Sparkfun-MP3-Player-Shield-Arduino-Library – Juraj Oct 27 '18 at 18:07
  • Thank You, I'll download and replace existing library. – Darryl Green Oct 27 '18 at 18:50
  • Did it. No change! – Darryl Green Oct 27 '18 at 19:06
  • so MP3ButtonPlayer2 example doesn't play track001.mp3? is the file a valid mp3 file? or you have some other sketch? if yes, add it to Question – Juraj Oct 28 '18 at 11:07
  • Yes MP3ButtonPlayer2 example doesn't play track001.mp3 that is a valid mp3 file. As explained above it does play using "File Player" when I serial input 0000X. – Darryl Green Oct 28 '18 at 18:35
  • and you have buttons connected to A0, A1, A2? And when you press button on A2 it prints "B_NEXT pressed, Start Playing Next Track #1" and doesn't play? – Juraj Oct 28 '18 at 18:43
  • Yes, buttons are connected to A0, A1, A2? And when A2 is pressed it prints "B_NEXT pressed, Start Playing Next Track #1" and doesn't play – Darryl Green Oct 28 '18 at 18:59
  • can you make a small modification to playTrack function in library? change char trackName[] = "track001.mp3"; to char trackName[13]; – Juraj Oct 28 '18 at 19:07
  • Where? Examples, keywords, SFEMP3Shield.cpp, SFEMP3Shield.h, SFEMP3ShieldConfig.h, SFEMP3Shieldmainpage.h – Darryl Green Oct 28 '18 at 19:32
  • SFEMP3Shield.cpp – Juraj Oct 28 '18 at 19:33
  • next you can try to change the parameter type from uint8_t trackNo to int trackNo, because that is right for sprintf. – Juraj Oct 28 '18 at 19:39
  • Okay, I found it in SFEMP3Shield.cpp and can only open in wordpad or notepad When I change it it won't let me save because I don't have administrator permission – Darryl Green Oct 28 '18 at 19:42
  • uint8_t trackNo to int trackNo is that in the sketch? – Darryl Green Oct 28 '18 at 19:43
  • I changed uint8_t track =0; to int track =0; in the sketch. No change. – Darryl Green Oct 28 '18 at 19:51
  • Okay, I opened SFEMP3Shield.cpp with visual studio and changed char trackName[] = "track001.mp3"; to char trackName[13]; No change. – Darryl Green Oct 29 '18 at 03:06
  • Do you know what this means? C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp: In member function 'uint8_t SFEMP3Shield::vs_init()':

    C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp:292:34: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    if(VSLoadUserCode("patches.053")) return 6;

    – Darryl Green Oct 29 '18 at 03:53
  • variable track is unused. you should change the parameter trackNo – Juraj Oct 29 '18 at 04:43
  • I'm sorry, I don't know what that means. – Darryl Green Oct 29 '18 at 04:53
  • There is no trackNo in the sketch – Darryl Green Oct 29 '18 at 04:55
  • the snippet in my answer. first line – Juraj Oct 29 '18 at 04:56
  • uint8_t SFEMP3Shield::playTrack(uint8_t trackNo){ to uint8_t SFEMP3Shield::playTrack(int trackNo){ – Juraj Oct 29 '18 at 04:57
  • There is no "trackNo" in the sketch are you referring to somewhere else? – Darryl Green Oct 29 '18 at 05:00
  • in SFEMP3Shield.cpp playTrack – Juraj Oct 29 '18 at 05:01
  • will not compile. 'uint8_t SFEMP3Shield::playTrack(int)' does not match any in class 'SFEMP3Shield' – Darryl Green Oct 29 '18 at 05:07
  • sorry, I forgot that it will require change in the .h. I add to answer a modified version of the playTrack function. try this – Juraj Oct 29 '18 at 05:20
  • I'm sorry, I can cut and paste, but I need to know what & where. – Darryl Green Oct 29 '18 at 05:24
  • from the Answer to SFEMP3Shield.cpp – Juraj Oct 29 '18 at 05:28
  • I don't know what the Answer to SFEMP3Shield.cpp is. – Darryl Green Oct 29 '18 at 05:31
  • copy/paste from answer, here on SE. my Answer to your Question. I edited it. scroll up – Juraj Oct 29 '18 at 05:33
  • I'm lost, Where is here on SE? – Darryl Green Oct 29 '18 at 05:34
  • https://arduino.stackexchange.com/questions/57147/playing-mp3-shield-tracks/57148#57148 – Juraj Oct 29 '18 at 05:37
  • still lost, I went through that before, you said will require change in the .h. – Darryl Green Oct 29 '18 at 05:41
  • which we do not make, instead we use the unused variable to have type int. sprintf doesn't like uint8_t – Juraj Oct 29 '18 at 05:43
  • I'm not a programmer, I don't know what any of that means, sorry. – Darryl Green Oct 29 '18 at 05:48
  • I'm sure I made all the changes you specified with the following result; C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp: In member function 'uint8_t SFEMP3Shield::vs_init()': C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp:292:34: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] if(VSLoadUserCode("patches.053")) return 6; C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp: At global scope: C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp:1025:9: – Darryl Green Oct 29 '18 at 06:23
  • error: prototype for 'uint8_t SFEMP3Shield::playTrack(int)' does not match any in class 'SFEMP3Shield' uint8_t SFEMP3Shield::playTrack(int trackNo){ In file included from C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.cpp:9:0: C:\Program Files (x86)\Arduino\libraries\SFEMP3Shield\SFEMP3Shield.h:691:13: error: candidate is: uint8_t SFEMP3Shield::playTrack(uint8_t) uint8_t playTrack(uint8_t); exit status 1 Error compiling for board Arduino/Genuino Uno. – Darryl Green Oct 29 '18 at 06:23
  • yes. the parameter must be uint8_t. revert this change. the first 'error' is only a warning. it was always there – Juraj Oct 29 '18 at 06:24
  • It seems I'm going in circles, we're right back where we were. Are you able to make the changes you want to SFEMP3Shield\SFEMP3Shield.cpp and give me a link to it so you can be sure it's the way you want it? I still believe the problem is the 5 digit index. Where is that generated? – Darryl Green Oct 29 '18 at 12:39
  • it is generated in FilePlayer example with SerialPrintPaddedNumber(count, 5 ); – Juraj Oct 29 '18 at 12:46
  • I cannot find SerialPrintPaddedNumber(count, 5 ); in FilePlayer example. – Darryl Green Oct 29 '18 at 17:44
  • I do find it in SFEMP3Shield\SFEMP3Shield.cpp Line 184& Line 621 – Darryl Green Oct 29 '18 at 17:50
  • I would expect the index to change when I change the 5 to 3 but it does not. – Darryl Green Oct 29 '18 at 17:51
  • Are you able to make the changes you want to SFEMP3Shield\SFEMP3Shield.cpp and give me a link to it so you can be sure it's the way you want it? – Darryl Green Oct 29 '18 at 17:54
  • in the Answer is the original and the modified version of playTrack function – Juraj Oct 29 '18 at 17:57
  • SerialPrintPaddedNumber is the last function in the sketch. this numbering is nothing general. it is the way the sketch uses to get user's choice – Juraj Oct 29 '18 at 18:04
  • Where is the Answer? – Darryl Green Oct 29 '18 at 19:16
  • we are commenting it here. scroll up – Juraj Oct 29 '18 at 19:24
  • reloaded SFEMP3Shield\SFEMP3Shield.cpp modified playTrack function from above. No progress, MP3ButtonPlayer2 does everything but play. I asked you before, do you know how to add leading zeros to a string? – Darryl Green Oct 30 '18 at 12:35
  • yes. the playTrack function does it as you can see in Answer and the SerialPrintPaddedNumber function in FilePlayer example does it too – Juraj Oct 30 '18 at 12:39