Can't spot anything obviously wrong with your code at a quick glance...
I suspect you've got some sort of pointer corruption somewhere overwriting the memory that the MP3 has been loaded into causing your issues...
Anyhow here's a copy of my working C++ code (Music class) from TetWiis that I've been developing.
This isn't by any means the prettiest code I've done
but it does work, other than there appears to be a slight timing issue at the end of MP3s where it loops too early.
I've just got around this by adding some silent seconds at the end of the file
Music.h
CODE/**
*
* Tetwiis
* (C)2009 http://www.pembo.co.uk
*
**/
//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#ifndef MUSIC_HPP
#define MUSIC_HPP
class Music
{
ÂÂÂÂpublic:
ÂÂÂÂÂÂÂÂMusic();
ÂÂÂÂÂÂÂÂ~Music();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂFILE *titleBGM;
ÂÂÂÂÂÂÂÂlong titlelSize;
ÂÂÂÂÂÂÂÂchar * titleBuffer;
ÂÂÂÂÂÂÂÂsize_t titleResult;
ÂÂÂÂÂÂÂÂbool titleMP3isready;
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂFILE *ingame1BGM;
ÂÂÂÂÂÂÂÂlong ingame1lSize;
ÂÂÂÂÂÂÂÂchar * ingame1Buffer;
ÂÂÂÂÂÂÂÂsize_t ingame1Result;
ÂÂÂÂÂÂÂÂbool ingame1MP3isready;ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂFILE *gameOverBGM;
ÂÂÂÂÂÂÂÂlong gameOverlSize;
ÂÂÂÂÂÂÂÂchar * gameOverBuffer;
ÂÂÂÂÂÂÂÂsize_t gameOverResult;
ÂÂÂÂÂÂÂÂbool gameOverMP3isready;ÂÂÂÂ
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid loadMusicIntoBuffer();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid initTitleMusic();
ÂÂÂÂÂÂÂÂvoid playTitleMusic();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid initIngame1Music();
ÂÂÂÂÂÂÂÂvoid playIngame1Music();
ÂÂÂÂÂÂÂÂvoid initGameOverMusic();
ÂÂÂÂÂÂÂÂvoid playGameOverMusic();ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid stop();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid mute();
ÂÂÂÂÂÂÂÂvoid unmute();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂprivate:
ÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid initialiseFat();
ÂÂÂÂÂÂÂÂbool can_open_root_fs();
};
#endif
Music.cpp
/**
CODE *
* Tetwiis
* (C)2009 http://www.pembo.co.uk
*
**/
//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
# include "Music.h"
# include
//------------------------------------------------------------------------------
// Externals
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Globals
//------------------------------------------------------------------------------
//_______________________________________________________________________________
/**
* Constructor
*/
Music::Music()
{
ÂÂÂÂinitialiseFat();
ÂÂÂÂASND_Init();
ÂÂÂÂMP3Player_Init();
ÂÂÂÂ
ÂÂÂÂtitleMP3isready = false;
ÂÂÂÂingame1MP3isready = false;
ÂÂÂÂloadMusicIntoBuffer();
ÂÂÂÂ
}
//_______________________________________________________________________________
/**
* destructor
*/
Music::~Music()
{
}
void Music::loadMusicIntoBuffer()
{
ÂÂÂÂMP3Player_Stop();
ÂÂÂÂ//if(titleBuffer)free(titleBuffer);
ÂÂÂÂtitleBGM = fopen("sd:/apps/tetwiis/music/title.mp3", "rb");
ÂÂÂÂif(titleBGM)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//Obtain file size
ÂÂÂÂÂÂÂÂfseek (titleBGM , 0 , SEEK_END);
ÂÂÂÂÂÂÂÂtitlelSize = ftell(titleBGM);
ÂÂÂÂÂÂÂÂrewind (titleBGM);
ÂÂÂÂÂÂÂÂ//Allocate memory to contain the whole file
ÂÂÂÂÂÂÂÂtitleBuffer = (char*) malloc (sizeof(char)*titlelSize);
ÂÂÂÂÂÂÂÂ//Copy the file into the buffer
ÂÂÂÂÂÂÂÂtitleResult = fread (titleBuffer,1,titlelSize,titleBGM);
ÂÂÂÂÂÂÂÂfclose(titleBGM);
ÂÂÂÂ}
ÂÂÂÂtitleMP3isready = true;
ÂÂÂÂ
ÂÂÂÂingame1BGM = fopen("sd:/apps/tetwiis/music/ingame1.mp3", "rb");
ÂÂÂÂif(ingame1BGM)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//Obtain file size
ÂÂÂÂÂÂÂÂfseek (ingame1BGM , 0 , SEEK_END);
ÂÂÂÂÂÂÂÂingame1lSize = ftell(ingame1BGM);
ÂÂÂÂÂÂÂÂrewind (ingame1BGM);
ÂÂÂÂÂÂÂÂ//Allocate memory to contain the whole file
ÂÂÂÂÂÂÂÂingame1Buffer = (char*) malloc (sizeof(char)*ingame1lSize);
ÂÂÂÂÂÂÂÂ//Copy the file into the buffer
ÂÂÂÂÂÂÂÂingame1Result = fread (ingame1Buffer,1,ingame1lSize,ingame1BGM);
ÂÂÂÂÂÂÂÂfclose(ingame1BGM);
ÂÂÂÂ}
ÂÂÂÂingame1MP3isready = true;
ÂÂÂÂ
ÂÂÂÂgameOverBGM = fopen("sd:/apps/tetwiis/music/gameover.mp3", "rb");
ÂÂÂÂif(gameOverBGM)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//Obtain file size
ÂÂÂÂÂÂÂÂfseek (gameOverBGM , 0 , SEEK_END);
ÂÂÂÂÂÂÂÂgameOverlSize = ftell(gameOverBGM);
ÂÂÂÂÂÂÂÂrewind (gameOverBGM);
ÂÂÂÂÂÂÂÂ//Allocate memory to contain the whole file
ÂÂÂÂÂÂÂÂgameOverBuffer = (char*) malloc (sizeof(char)*gameOverlSize);
ÂÂÂÂÂÂÂÂ//Copy the file into the buffer
ÂÂÂÂÂÂÂÂgameOverResult = fread (gameOverBuffer,1,gameOverlSize,gameOverBGM);
ÂÂÂÂÂÂÂÂfclose(gameOverBGM);
ÂÂÂÂ}
ÂÂÂÂgameOverMP3isready = true;ÂÂÂÂ
}
void Music::initTitleMusic()
{
ÂÂÂÂMP3Player_PlayBuffer(titleBuffer,titlelSize,NULL);
}
void Music::initIngame1Music()
{
ÂÂÂÂMP3Player_PlayBuffer(ingame1Buffer,ingame1lSize,NULL);
}
void Music::initGameOverMusic()
{
ÂÂÂÂMP3Player_PlayBuffer(gameOverBuffer,gameOverlSize,NULL);
}
void Music:
layTitleMusic()
{
ÂÂÂÂif( (!MP3Player_IsPlaying() && titleMP3isready))
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂMP3Player_PlayBuffer(titleBuffer, titlelSize, NULL);
ÂÂÂÂ}
}
void Music:
layIngame1Music()
{
ÂÂÂÂif( (!MP3Player_IsPlaying() && ingame1MP3isready))
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂMP3Player_PlayBuffer(ingame1Buffer, ingame1lSize, NULL);
ÂÂÂÂ}
}
void Music:
layGameOverMusic()
{
ÂÂÂÂif(MP3Player_IsPlaying() && (gameOverMP3isready))
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂMP3Player_PlayBuffer(gameOverBuffer, gameOverlSize, NULL);
ÂÂÂÂ}
}
void Music::stop()
{
ÂÂÂÂMP3Player_Stop();
}
//_______________________________________________________________________________
/*
* Can open the SD Card
*/
bool Music::can_open_root_fs()
{
ÂÂÂÂDIR_ITER *root = diropen("SD:/");
ÂÂÂÂif (root)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂdirclose(root);
ÂÂÂÂÂÂÂÂreturn true;
ÂÂÂÂ}
ÂÂÂÂreturn false;
}
//_______________________________________________________________________________
/*
* Initialises FAT
*/
void Music::initialiseFat()
{
ÂÂÂÂ//printf(" Initialising FAT ......... ");
ÂÂÂÂif (!fatInitDefault())
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//drawError(15,11,11,50,err1_initFatErr);
ÂÂÂÂÂÂÂÂ//errorApplication("Unable to initialise FAT subsystem, exiting.\n");
ÂÂÂÂ}
ÂÂÂÂ
ÂÂÂÂif (!can_open_root_fs())
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//errorApplication("Unable to open SD card, exiting.\n");
ÂÂÂÂÂÂÂÂ//drawError(15,11,11,50,err2_rootFS);
ÂÂÂÂ}
ÂÂÂÂ//printf("Done\n");
}
void Music::mute()
{
ÂÂÂÂMP3Player_Volume(0);
}
void Music::unmute()
{
ÂÂÂÂMP3Player_Volume(254);
}
And here is a video of it in action playing the music...