SpresenseでMP3を連続再生したいのですが、連続再生をしていると途中で以下のエラーが出てきてしまいます。
ちなみに書いているコードはこちらです。
#include <Audio.h>
#include <SDHCI.h>
SDClass SDcard;
AudioClass *audio;
File dir, file;
err_t err;
int TrackCounter = 0; // MP3 Playing Track Counter.
bool PlayingState = false; // true MP3 Playing.
bool setPlayState = false;
int AudioVolume = -220;
bool ErrEnd = false;
static void audio_attention_cb(const ErrorAttentionParam *atprm) {
puts("Attention!");
if (atprm->error_code >= AS_ATTENTION_CODE_WARNING) {
ErrEnd = true;
}
}
void setup() {
SDcard.begin();
// Setup AudioPlayer.
// DSP File -> SPIFLASH.
audio = AudioClass::getInstance();
audio->begin(audio_attention_cb);
audio->setRenderingClockMode(AS_CLKMODE_NORMAL);
audio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);
audio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/spif/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_STEREO);
Serial.begin(115200); // Debug
}
/* Audio Volume UP */
void AudioVolume_UP() {
if (AudioVolume >= -220) { // Volume Max Value
Serial.println("Volume is Max value!");
} else {
AudioVolume = AudioVolume + 20;
Serial.println(AudioVolume);
audio->setVolume(AudioVolume);
}
}
/* Audio Volume Down */
void AudioVolume_DOWN() {
if (AudioVolume <= -700) { // Volume Min Value
Serial.println("Volume is Min value!");
} else {
AudioVolume = AudioVolume - 20;
Serial.println(AudioVolume);
audio->setVolume(AudioVolume);
}
}
void NextMusicPlay() {
if (PlayingState == true) {
audio->stopPlayer(AudioClass:: Player0);
PlayingState = false;
AudioPlay_fop();
} else {
/* - */
}
}
void AudioStopStartPlay() {
if (PlayingState == true) {
audio->stopPlayer(AudioClass:: Player0, AS_STOPPLAYER_ESEND);
PlayingState = false;
setPlayState = false;
} else {
if (ErrEnd) {
ErrEnd = false;
audio->begin(audio_attention_cb);
audio->setRenderingClockMode(AS_CLKMODE_NORMAL);
audio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);
audio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/spif/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_STEREO);
usleep(40000);
}
AudioPlay_fop();
setPlayState = true;
}
}
/* AudioPlay, File Open */
void AudioPlay_fop() {
if (!dir) {
dir = SDcard.open("/Musics");
}
file = dir.openNextFile();
if (!file) {
dir.close();
dir = SDcard.open("/Musics");
file = dir.openNextFile();
TrackCounter = 0;
}
// puts(file.name());
audio->writeFrames(AudioClass:: Player0, file);
usleep(40000);
audio->startPlayer(AudioClass:: Player0);
PlayingState = true;
TrackCounter ++;
String trstr = "Track: " + String(TrackCounter);
Serial.println(trstr);
audio->setVolume(AudioVolume);
}
void loop() {
if (setPlayState == true) {
/*if (!file) {
AudioPlay_fop();
}*/
err = audio->writeFrames(AudioClass:: Player0, file);
if (err == AUDIOLIB_ECODE_FILEEND) {
audio->stopPlayer(AudioClass:: Player0, AS_STOPPLAYER_ESEND);
PlayingState = false;
AudioPlay_fop();
}
if (ErrEnd) {
audio->stopPlayer(AudioClass:: Player0);
PlayingState = false;
setPlayState = false;
file.close();
audio->setReadyMode();
audio->end();
}
usleep(1000);
} else {
/* - */
}
if (Serial.available() > 0){ // Test Mode.
Serial.println("read");
String serstr = Serial.readString();
serstr.trim();
if (serstr.equals("up") == true) {
AudioVolume_UP();
}
if (serstr.equals("down") == true) {
AudioVolume_DOWN();
}
if (serstr.equals("ps") == true) {
AudioStopStartPlay();
}
if (serstr.equals("next") == true) {
NextMusicPlay();
}
}
}
いろいろ検索してみたのですが、解決方法が分かりません...
なにか間違っているところなどありましたら教えてください。お願いしますm(_ _)m