0

(2015-12-06) I'm using Visual Studio 2013 C# and I'm wondering how I can let the program play an MP3 file as a background music, even when it's distributed to someone else's computer (and not just on my computer with the my own path of the MP3 file - e.g. c:\my documents\so and so music.mp3).

(2015-12-07) Thanks to Maximilian Gerhardt for the kind reply. But when I tried with my own out.wav, I got stuck with a few red lines.. (compile errors..) And I'm not so sure how to go on. (sorry I'm new to C#) This is what I see - the image of the compile error screen

1 Answers1

0

First you create a new resource file somewhere in your project. Use the Add -> New element context menu in the project explorer to do that: enter image description here (yes, sorry, that's in german.)

Once you created your resource file you can double-click on it in the project explorer and add resources to it. I added a .wav file called out.wav. This could be your .mp3 file. abc Next you just have to access that stream in Code. Adding the resource file also generates some code which let's you access that file as a stream. Usage e.g.:

using System;
using System.IO;
using System.Text;

namespace ResourceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a memory stream to read the file into..
            MemoryStream outStream = new MemoryStream();
            //Copy our _out.wav File
            SomeResource._out.CopyTo(outStream);
            //Also possible: 
            //SomeResource.ResourceManager.GetStream("out.wav")...
            //Read into a byte array.
            byte[] file_bytes = outStream.ToArray();
            //Close it
            outStream.Close();

            //Do something with file_bytes..
        }
    }
}

Now that you've embedded your ressource in a ressource file, it's always accessible (i.e. the resource always gets copied in your compiled binary)

Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
  • Thanks for help. But unfortunately.. couldn't get it to work.. (please see the attached) – Ho Dong Kim Dec 07 '15 at 11:23
  • The class name `SomeResource` was derived from the name of the resource file I created (`SomeResource.resx`). You must change the classname to the filename of the resource file you created. (There's a `Resources.resx`, so you should try `Resources._out`?) – Maximilian Gerhardt Dec 07 '15 at 16:33
  • Ah ha. So that was it. I re-wrote the code and it compiles well with no errors. But then.... it plays no sounds either.. It looks like we managed to load the resource onto a memory, but how do we play the resource from the memory...? Sorry for asking too many questions. – Ho Dong Kim Dec 07 '15 at 18:00
  • You can use the library `NAudio` to create an audio device and fill it with data, which can e.g. be from the `file_bytes[]`. See http://stackoverflow.com/a/15025797/5296568 and http://stackoverflow.com/q/184683/5296568 . – Maximilian Gerhardt Dec 07 '15 at 18:23
  • Thanks again :). Maximilian. You're awesome – Ho Dong Kim Dec 08 '15 at 18:50