Playing MP3 songs form SD card

I got one FEZ Music Shield and the overview states: [italic]Few simple lines of code are plenty enough to play music from SD cards, USB memory stick or even stream audio over the network[/italic].

Can someone please supply me with a simple working example on how to play a normal 4Mb MP3 file from my FEZ Domino SD card?


Geir, is this what you are looking for:

Not completely. The example only streams a small file that has been preloaded from resources. I need an example of reading a normal size MP3 file from an SD card.

Do you know how to read files from SD? GHI Electronics – Where Hardware Meets Software

If so, all you need is this

while(file is not done)
WriteTo MusicShield(…);

This is what I have tried

    public class Program
        static MusicShield musicShield = new MusicShield(SPI.SPI_module.SPI1, FEZ_Pin.Digital.An4, FEZ_Pin.Digital.An5, FEZ_Pin.Digital.Di4);
        public static void Main()
            PersistentStorage sd = new PersistentStorage("SD");

            while (true)



        public static void PlayStatement(string FileName)
            int offset = 0;
            byte[] data = new byte[10 * 1024];
            musicShield.SetVolume(240, 240);

            FileStream fs = new FileStream("\\SD\\" + FileName , FileMode.Open, FileAccess.Read);
            fs.Seek(offset, SeekOrigin.Begin);

            int bytesRead = 0;
            bytesRead = fs.Read(data, 0, data.Length);

            while (bytesRead != 0)
                while (musicShield.IsBusy) Thread.Sleep(10); 
                bytesRead = fs.Read(data, 0, data.Length); 
            fs = null;


It plays the file… sort of. But there is a small glitch in the sound output when new data is read, so the quality is crap. How to fix this?

did you tried using a smaller buffer?

What is the bit-rate of the file you are playing? Lower bit-rate will make things a lot easier.

Also, if I remember correctly the internal buffer of the MP3 chip is 32bytes or something really small so reading 1K data maybe to long of a delay to the MP3 chip. Use smaller buffer like Eric suggested.

Ok so I tried a lower bitrate 160kbps (the shield overview doesnt say anything about bitrate limitations). That did not help.

I tried with a buffersize from 16byte, 32byte, 512byte of 1k and got absoluttly nothing out of the speakers.
Tried with a buffer of 5k got some very copped up ugly sound.
With a 10k buffer Im able to hear what song it is, -but still crap (lots of chopping).

I found an example that seems to work
But I cant figure out this line

musicShield.OnPlaybackStopped += new GHIElectronics.NETMF.FEZ.Shields.MusicShield.PlaybackStopped(musicShield_OnPlaybackStopped);

The compiler states: The name ‘musicShield_OnPlaybackStopped’ does not exist in the current context

Is there something missing in the example?


delete everything starting from +=, add again += and press tab twice.

Visual studio adds the missing code for you when you do the above.

Thanks EriSan :slight_smile:

Works now?

Yes it works now.
Thanks for your help.