RePhone APIs - Audio

Introduction

RePhone supports playback audio files on SD card or on built-in flash. Formats supported include WAVMP3AAC, etc. With this library, you can PlayPauseStop playback process and Adjust volume.
The playback (decoding) job is handled internally by LinkIt. Therefore, all functions are non-blocking, which means after you call playFile(), it will play the audio and immediately return to your program.
Use getStatus() to check the current status and act correspondingly. The audio status can be “playing”, “play finished”, “play paused”, etc. The audio will be outputted through the earphone jack on the Linkit board.
Note
If you try to play another audio file before the previous playback is finished, the previous playback will be stopped automatically, and the new audio will be played.

Function List

  • begin()
  • playFile()
  • setVolume()
  • pause()
  • resume()
  • stop()
  • getStatus()

APIs

LAudio.begin()

Initializes the Audio module.
  • Syntax
LAudio.begin()
  • Parameters
N/A
  • Returns
N/A
  • Example
#include <LAudio.h>   void setup() { LAudio.begin(); LAudio.setVolume(2); LAudio.playFile(storageFlash,(char*)"sample.mp3"); }   void loop() {   }

LAudio.playFile()

Plays an audio file on SD card or flash storage.
  • Syntax
LAudio.playFile(StorageEnum drv, char *songName) LAudio.playFile(StorageEnum drv, wchar_t* songName)
  • Parameters
    • drv - Storage, can be storageSD (SD card) or storageFlash (flash storage)
    • songName - Path of audio file in ascii (char*) or unicode (wchar_t*) encoding
  • Returns
N/A
  • Example
#include <LAudio.h>   void setup() { LAudio.begin(); LAudio.setVolume(2); LAudio.playFile(storageFlash,(char*)"sample.mp3"); }   void loop() {   }

LAudio.setVolume()

Adjusts the playback volume; range is from 0 (silent) to 6 (max).
  • Syntax
LAudio.setVolume(unsigned char volume)
  • Parameters
    • volume: Volume level, can be 0 to 6; 0 as silent and 6 as max volume
  • Returns
N/A
  • Example
#include <LAudio.h>   void setup() { LAudio.begin(); LAudio.setVolume(2); LAudio.playFile(storageFlash,(char*)"sample.mp3"); }   void loop() {   }

LAudio.pause()

Pauses the playback. It only works when there is an active playback ongoing (by playFile()).
  • Syntax
LAudio.pause()
  • Parameters
N/A
  • Returns
N/A
  • Example
#include <LAudio.h>   void setup() { LAudio.begin(); LAudio.playFile(storageFlash,(char*)"sample.mp3"); LAudio.setVolume(2); delay(5000); LAudio.pause(); // play for 5secs and pause   delay(5000); LAudio.resume(); // wait for another 5 secs to resume   delay(5000); LAudio.stop(); // wait for another 5 secs to stop }   void loop() {   }

LAudio.resume()

Resumes playback. It only works when the playback has been paused.
  • Syntax
LAudio.resume()
  • Parameters
N/A
  • Returns
N/A
  • Example
#include <LAudio.h> void setup() { LAudio.begin(); LAudio.playFile(storageFlash,(char*)"sample.mp3"); LAudio.setVolume(2); delay(5000); LAudio.pause(); // play for 5secs and pause   delay(5000); LAudio.resume(); // wait for another 5 secs to resume   delay(5000); LAudio.stop(); // wait for another 5 secs to stop }   void loop() {   }

LAudio.stop()

Stops playback.
  • Syntax
LAudio.stop()
  • Parameters
N/A
  • Returns
N/A
  • Example
#include <LAudio.h>   void setup() { LAudio.begin(); LAudio.playFile(storageFlash,(char*)"sample.mp3"); LAudio.setVolume(2); delay(5000); LAudio.pause(); // play for 5secs and pause   delay(5000); LAudio.resume(); // wait for another 5 secs to resume   delay(5000); LAudio.stop(); // wait for another 5 secs to stop }   void loop() {   }

LAudio.getStatus()

Queries the current playback status.
  • Syntax
LAudio.getStatus()
  • Parameters
N/A
  • Returns
    • AudioStatus - Playback status, can be:
    • AudioStop - Playback is stopped.
    • AudioPause - Playback is paused (and can resume).
    • AudioResume - Playback resumes
    • AudioEndOfFile - Playback is finished.
    • AudioCommonFailed - Playback fails (e.g. the audio file is corrupted).
  • Example
#include <LAudio.h>   void setup() { LAudio.begin(); LAudio.setVolume(2); LAudio.playFile(storageFlash,(char*)"sample.mp3"); }   void loop() { AudioStatus status; status = LAudio.getStatus(); if(AudioEndOfFile == status) // Check if playback finish and restart { LAudio.playFile(storageFlash,(char*)"sample.mp3"); } }

Example

  • Description
With this example, you can play a mp3 file. Open serial monitor to input a number to control the playing.
Command as below:
  1. Play
  2. Setvolume
  3. Pause
  4. Resume
  5. Stop
  6. Code
You can find the code here: File -> Examples -> LAudio -> AudioPlayer
/* RePhone audio test Demo First you need to put a mp3 file to storage Then open the Serial monitor, input number to change the status 1 - Play 2 - Setvolume 3 - Pause 4 - Resume 5 - Stop */ #include <LAudio.h>   // add your music file name here #define FILE_NAME "music.mp3"   #define PLAY 1 #define SETVOLUME 2 #define PAUSE 3 #define RESUME 4 #define STOP 5   unsigned char Status_Value = STOP;   int volume = 6;   void changeVolume() { volume++; volume = volume>6 ? 1 : volume; }   void setup() { LAudio.begin(); Serial.begin(115200); Serial.println("Play - 1"); Serial.println("Setvolume - 2"); Serial.println("Pause - 3"); Serial.println("Resume - 4"); Serial.println("Stop - 5"); Change_Status(PLAY); }   char StatusFlag = 0;   void loop() { unsigned char KEY_NUM; AudioStatus status;   KEY_NUM = task_uart_key();   if(KEY_NUM > 0) { Change_Status(KEY_NUM); KEY_NUM = 0; }   status = LAudio.getStatus(); if(StatusFlag != status) { StatusFlag = status; Serial.print("LAudio status is "); Serial.println(status); }   if(status == AudioEndOfFile) { Status_Value = PLAY; Change_Status(Status_Value); } }   void Change_Status(unsigned char status) { switch(status) { case 1: LAudio.playFile(storageFlash,(char*)"music.mp3"); LAudio.setVolume(volume); Serial.println("playOne"); break;   case 2: LAudio.setVolume(volume); changeVolume(); Serial.print("set volume to "); Serial.println(volume); break;   case 3: LAudio.pause(); Serial.println("pause"); break;   case 4: LAudio.resume(); Serial.println("resume"); break;   case 5: LAudio.stop(); Serial.println("stop"); break;   default: break; } }   unsigned int task_uart_key() { String inString = ""; unsigned int keyValue = 0; unsigned char bitCount = 0; unsigned char dataTemp1[10] = {0}; while(Serial.available() > 0) { unsigned char inChar = Serial.read(); inString += (char)inChar; dataTemp1[bitCount] = inChar - '0'; bitCount += 1; delay(10); } if(inString != "") { if(bitCount > 4) { Serial.println("Key input error."); } else { for(char i=0;i<bitCount;i++) { unsigned int dataTemp2 = 1; for(char j=0;j<(bitCount-i-1);j++)dataTemp2 *= 10; keyValue += (dataTemp1[i] * dataTemp2); } Serial.print("Key value is: "); Serial.println(keyValue); } } return keyValue; }

Resources

Help us make it better

Thank you for choosing Seeed. A couple of months ago we initiated a project to improve our documentation system. What you are looking at now is the first edition of the new documentation system. Comparing to the old one, here is the progresses that we made:
  • Replaced the old documentation system with a new one that was developed from Mkdocs, a more widely used and cooler tool to develop documentation system.
  • Integrated the documentation system with our official website, now you can go to Bazaar and other section like Forum and Community more conveniently.
  • Reviewed and rewrote documents for hundreds of products for the system’s first edition, and will continue migrate documents from old wiki to the new one.
An easy-to-use instruction is as important as the product itself. We are expecting this new system will improve your experience when using Seeed’s products. However since this is the first edition, there are still many things need to improve, if you have any suggestions or findings, you are most welcome to submit the amended version as our contributor or give us suggestions in the survey below, Please don’t forget to leave your email address so that we can reply.
Happy hacking