Problem with GHI Music Module

Hello.

I have a very big problem with my GHI Music Module.

I can’t get’s it to play music.

My setup looks like the attached image.

My code looks like this:

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.IO;
using System.Text;

namespace TeddyBear
{
    public partial class Program
    {

        private GT.StorageDevice mysdcard;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {

            sdCard.MountSDCard();

            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
            sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);





            mysdcard = sdCard.GetStorageDevice();

            byte[] playmusic = mysdcard.ReadFile("music.mp3");

            music.Play(playmusic);
   

        }
 
void sdCard_SDCardUnmounted(SDCard sender)
{
    Debug.Print("The SD card has been unmounted");
    Debug.Print("DO NOT try to access it without mounting it again first");
}
 
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
{
    Debug.Print("SD card has been successfully mounted. You can now read/write/create/delete files");
    Debug.Print("Unmount before removing");
}
        }
    }


But every time I run my program i gets a System.IO.IOException in this line:

   byte[] playmusic = mysdcard.ReadFile("music.mp3");

My Output view in Visual Studio looks like this:

Found debugger!

Create TS.

 Loading start at 805b980, end 808459c

   Assembly: mscorlib (4.2.0.0)     Assembly: Microsoft.SPOT.Native (4.2.0.0)     Assembly: Microsoft.SPOT.Hardware (4.2.0.0)  
   Assembly: Microsoft.SPOT.Graphics (4.2.0.0)     Assembly: Microsoft.SPOT.TinyCore (4.2.0.0)  
   Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0)     Assembly: Microsoft.SPOT.IO (4.2.0.0)  
   Assembly: System.IO (4.2.0.0)     Assembly: Microsoft.SPOT.Hardware.OneWire (4.2.0.0)  
   Assembly: Microsoft.SPOT.Hardware.Usb (4.2.0.0)     Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1)  
   Assembly: Microsoft.SPOT.Net (4.2.0.0)     Assembly: System (4.2.0.0)  Loading Deployment Assemblies.

Attaching deployed file.

   Assembly: GHI.OSHW.Hardware (4.2.3.1)  Attaching deployed file.

   Assembly: Gadgeteer (2.42.0.0)  Attaching deployed file.

   Assembly: System.Http (4.2.0.0)  Attaching deployed file.

   Assembly: GTM.GHIElectronics.SDCard (1.1.1.0)  Attaching deployed file.

   Assembly: TeddyBear (1.0.0.0)  Attaching deployed file.

   Assembly: Microsoft.SPOT.Net.Security (4.2.0.0)  Attaching deployed file.

   Assembly: GHIElectronics.Gadgeteer.FEZCerberus (1.1.1.0)  Attaching deployed file.

   Assembly: Microsoft.SPOT.Touch (4.2.0.0)  Attaching deployed file.

   Assembly: GTM.GHIElectronics.Music (1.1.1.0)  Attaching deployed file.

   Assembly: System.Net.Security (4.2.0.0)  Attaching deployed file.

   Assembly: Gadgeteer.SPI (2.42.0.0)  Attaching deployed file.

   Assembly: GTM.GHIElectronics.UC_Battery_4xAA (1.1.1.0)  Resolving.

The debugging target runtime is loading the application assemblies and starting execution.
Ready.

'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Native.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Graphics.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.TinyCore.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.OneWire.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.Usb.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.PWM.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI OSHW NETMF v4.2 SDK\Assemblies\le\GHI.OSHW.Hardware.dll'
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Gadgeteer\Core\Assemblies\.NET Micro Framework 4.2\le\Gadgeteer.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.Security.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Net.Security.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Http.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\SDCard\NETMF 4.2\le\GTM.GHIElectronics.SDCard.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\UC_Battery_4xAA\NETMF 4.2\le\GTM.GHIElectronics.UC_Battery_4xAA.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Gadgeteer\Core\Assemblies\.NET Micro Framework 4.2\le\Gadgeteer.SPI.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\Music\NETMF 4.2\le\GTM.GHIElectronics.Music.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI .NET Gadgeteer SDK\Mainboards\FEZCerberus\NETMF 4.2\le\GHIElectronics.Gadgeteer.FEZCerberus.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Users\Jacob\Documents\Visual Studio 2010\Projects\TeddyBear\TeddyBear\bin\Debug\le\TeddyBear.exe', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Touch.dll', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
Failed allocation for 172 blocks, 2064 bytes

A first chance exception of type 'System.IO.IOException' occurred in System.IO.dll
A first chance exception of type 'System.IO.IOException' occurred in System.IO.dll
An unhandled exception of type 'System.IO.IOException' occurred in System.IO.dll


Can anyone help me PLEASE…

Best Regards
Jacob Lau

Hi Jacob - Welcome :slight_smile:

I’m guessing it’s because you haven’t passed the name of the directory - have a look at this code:

//Mount the SD card
sdCard.MountSDCard();
//Get the root directory
string rootDirectory = sdCard.GetStorageDevice().RootDirectory;
//Use Streamwriter to write a text file
StreamWriter textFile = new StreamWriter(rootDirectory + @ "\hello.txt");
textFile.WriteLine("Hello SD card");
textFile.Close();

Cheers
Justin

Hi Justin.

When i try this code i still get’s an System.IO.IOException error in this line.

StreamWriter textFile = new StreamWriter(rootDirectory + @ "\hello.txt");

Create a small text file with a few lines in it called file.txt and pop it in the root on the SD and see if this code works (not tested)

 

 sdCard.MountSDCard();
            string _root = sdCard.GetStorageDevice().RootDirectory;
string fileName = Path.Combine(_root, "file.txt");
            FileStream fileStream = new FileStream(fileName, FileMode.Open);
            byte[] data = new byte[fileStream.Length];
            fileStream.Read(data, 0, data.Length);
            fileStream.Close();
            Debug.Print(new string(Encoding.UTF8.GetChars(data)));

Hi Justin.

Still System.IO.IOException in this line:

FileStream fileStream = new FileStream(fileName, FileMode.Open);

The funny thing is when I look in the FileStream, the value is Null. So I don’y think that the FileStream can load the file from the SD Card.

I feel like I want to throw all this shit in the trash right now… I’am so frustrated.

Can you help me…?

Hi Jacob, fear not, once the penny drops it works a treat.

Give me and hour or so and i will actually test and send you some code.

Don’t throw it in the bin just yet :wink:

THANK YOU JUSTIN… :wink:

I’ll wait for your reply.

Hi Jacob,

Tested and working on a Spider

using System;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace BeeMusicTest
{
    public partial class Program
    {
        private string _root;
        private static FileStream _fs;
          
        void ProgramStarted()
        {
            sdCard.MountSDCard();
            _root = sdCard.GetStorageDevice().RootDirectory;

            music.musicFinished += new Music.MusicFinishedPlayingEventHandler(music_musicFinished);
            music.SetVolume(255);

            button.ButtonReleased += new Button.ButtonEventHandler(ButtonButtonReleased);
        }

        void ButtonButtonReleased(Button sender, Button.ButtonState state)
        {
            music.Play(LoadMusicFileBytes("1.mp3"));
        }

        private byte[] LoadMusicFileBytes(string mp3)
        {
            string fileName = Path.Combine(_root, mp3);
            try
            {
                _fs = new FileStream(fileName, FileMode.Open);
                byte[] b = new byte[_fs.Length];
                _fs.Read(b, 0, (int)_fs.Length);
                _fs.Dispose();
                return b;
            }
            catch (Exception ex)
            {


            }
            return null;
        }
        void music_musicFinished(Music sender)
        {
            Debug.Print("Finished");
        }
    }
}

Note: you might need to keep the files less then ~768kb with this test…

Hi Justin.

Thank you.

When I run the Program now, Visual Studio ask’s me for a class called Music_42.cs

Check the attached image.

What the heck is that…?

Sounds like its asking for the source file for the music driver.

And where can I find that.?

You can download all the source files off codeplex. Google gadgeteer codeplex, on phone so it’s a bit hard.

Since it wants the code sounds like something is nit quite right.

Paste the code you are running.

http://gadgeteer.codeplex.com/SourceControl/changeset/view/24955#256548

Here is my code Justin.

using System;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace New_TeddyBear
{
    public partial class Program
    {
        private string _root;
        private static FileStream _fs;

        void ProgramStarted()
        {

            
            sdCard.MountSDCard();
            _root = sdCard.GetStorageDevice().RootDirectory;

            music.musicFinished += new Music.MusicFinishedPlayingEventHandler(music_musicFinished);
            music.SetVolume(255);
            music.Play(LoadMusicFileBytes("1.mp3"));


        }


   

        private byte[] LoadMusicFileBytes(string mp3)
        {
            string fileName = Path.Combine(_root, mp3);
            try
            {
                _fs = new FileStream(fileName, FileMode.Open);
                byte[] b = new byte[_fs.Length];
                _fs.Read(b, 0, (int)_fs.Length);
                _fs.Dispose();
                return b;
            }
            catch (Exception ex)
            {


            }
            return null;
        }
        void music_musicFinished(Music sender)
        {
            Debug.Print("Finished");
        }
    }
}

I found the Music_42.cs class file and added it to my project, and afterwards when I run the program I get’s this error:

An unhandled exception of type ‘System.NullReferenceException’ occurred in GTM.GHIElectronics.Music.dll

You don’t need to add the file.
You shouldn’t run the music.play from program start…
If you step thru the code with degugger is the file found?
How big is the file?

If I delete the Music_42.cs class file, from my project, and try to run the program this is what I get…

See attached image

My Music file is 88 KB

Where should I so run my Music.Play function…?

I don’t have a button module like you…

Do you have a button module?
If not you could use a timer.

And how precise can I use a Timer…

What should I write in my code…?