B4R Question Mp3 Player

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please try this:

1. Download the attached zip file and copy both files to rSD libraries folder (Program Files (x86)\Anywhere Software\B4R\Libraries\rSD).
2. Run this code:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private sd As SD
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   sd.Initialize(4)
   RunNative("begin", Null)
   StartPlaying("track002.mp3")
End Sub

Sub StartPlaying(File As String)
   RunNative("startPlayingFile", File)
End Sub

#if C
#include "Adafruit_VS1053.h"
#define BREAKOUT_RESET  9  // VS1053 reset pin (output)
#define BREAKOUT_CS  10  // VS1053 chip select pin (output)
#define BREAKOUT_DCS  8  // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1  // VS1053 reset pin (unused!)
#define SHIELD_CS  7  // VS1053 chip select pin (output)
#define SHIELD_DCS  6  // VS1053 Data/command select pin (output)
#define CARDCS 4  // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3  // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
  Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
   void begin(B4R::Object* o) {
     if (! musicPlayer.begin()) { // initialise the music player
      Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
      while (1);
       }
       Serial.println(F("VS1053 found"));
      musicPlayer.setVolume(20,20);
      musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);
   }
   void startPlayingFile (B4R::Object* o) {
     musicPlayer.startPlayingFile((const char*)B4R::Object::toPointer(o));
   }
#end if

I don't have this shield here so I cannot test it.
 

Attachments

  • Adafruit_VS1053.zip
    6.5 KB · Views: 340
Upvote 0

stp

Member
Licensed User
Longtime User
Erel I would like to manage the sound volume during the duration that the song is currently playing.
There are also some other commands for stoping, pausing the mp3.
How can i realize that ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this code to the inline c code:
B4X:
void SetVolume(B4R::Object* o) {
 musicPlayer.setVolume((Byte)o->toULong(), (Byte)o->toULong());
}
void StopPlaying(B4R::Object* o) {
 musicPlayer.stopPlaying();
}
void Pause(B4R::Object* o) {
 musicPlayer.pausePlaying((boolean)o->toULong());
}

You can now call it with:
RunNative("SetVolume", 50)
RunNative("StopPlaying", Null)
 
Upvote 0
Top