2

Hello I am new at c# and I am doing a small game that I need to play mp3 files.

I've been searching about this and using wmp to do it, like this:

    WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
    myplayer.URL = @"c:\somefolder\project\music.mp3";
    myplayer.controls.play();

I am able to play the file successfully with the full path of the mp3 file. The problem is that I can't find a way to use the file directly from the project folder, I mean, if I copy the project to another computer the path of the mp3 file will be invalid and no sound will be played. I feel that I am at a dead end now, so if someone can help me I would appreciate it! Thanks in advance

ERS
  • 53
  • 1
  • 6

3 Answers3

0

Add the MP3 file to your project. Also flag it to always copy to output folder. Here you have a tutorial of how to do it (How to include other files to the output directory in C# upon build?). Then you can reference this way:

You have to use:

using System.Windows.Forms;

And then you can use like this:

WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = Application.StartupPath + "\music.mp3";
myplayer.controls.play();
Community
  • 1
  • 1
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • Hi, just had to add one more backslash like this "\\music.mp3" and now it works like a charm! Thanks you! – ERS Feb 10 '17 at 17:57
  • you got it. dont forget to mark as answer and upvote if it helped. :) – NicoRiff Feb 10 '17 at 18:04
  • Done that, because my reputation is less than 15 it will not be displayed publicly I guess. – ERS Feb 10 '17 at 18:06
0

This should work for any machine, provided your mp3 & exe are in same folder.

  string mp3Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + mp3filename
Shashi Bhushan
  • 1,292
  • 11
  • 15
0

Another simple option to use would be:

WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
string mp3FileName = "music.mp3";
myplayer.URL = AppDomain.CurrentDomain.BaseDirectory + mp3FileName;
myplayer.controls.play();

This will play the MP3 from the directory that your executable is located in. It is also important to note that no reflection is needed, which can add unnecessary performance cost.

As a follow up to the comment about embedding the MP3 as a resource, the following code can be implemented once it has been added:

Assembly assembly = Assembly.GetExecutingAssembly();
string tmpMP3 = AppDomain.CurrentDomain.BaseDirectory + "temp.mp3";
using (Stream stream = assembly.GetManifestResourceStream("YourAssemblyName.music.mp3"))
using (Stream tmp = new FileStream(tmpMP3, FileMode.Create))
{
    byte[] buffer = new byte[32 * 1024];
    int read;

    while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        // Creates a temporary MP3 file in the executable directory
        tmp.Write(buffer, 0, read);
    }
}
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = tmpMP3;
myplayer.controls.play();
// Checks the state of the player, and sends the temp file path for deletion
myplayer.PlayStateChange += (NewState) =>
{
    Myplayer_PlayStateChange(NewState, tmpMP3);
};

private static void Myplayer_PlayStateChange(int NewState, string tmpMP3)
{
    if (NewState == (int)WMPPlayState.wmppsMediaEnded)
    {
        // Deletes the temp MP3 file
        File.Delete(tmpMP3);
    }
}
Kevin Mills
  • 408
  • 10
  • 15
  • Thanks for your help with one more way to do it! By the way, I notice that that file can be embedded to the exe file right? On properties / building action - embedded resource ? If I am right, how can I call it to myplayer.URL ? – ERS Feb 10 '17 at 20:37
  • Once again, Thanks! – ERS Feb 10 '17 at 22:17