2

I know there are a dozen questions like this and I looked at the more popular ones, but I have had no luck in figuring out anything.

I want to make a C# *.exe that would play a sound when opened, I decided it would be OK to start with a Console Application project, but I have no idea if that is the best choice.

I have added the "file.mp3" file to the resource designer under "Other" files and set its build action to "Embedded Resource".

From there I saw the suggestion about WMPLib, which I cannot access in my project - it is not present neither in my Reference Manager, neither does "using WMPLib;" work (it raises a "could not be found" error) or the one about the "System.Media.SoundPlayer.Play();" method but I am having trouble implementing it, because my complier either says an object reference is required for it or it has no overload, because it only takes 1 arguments if I make it like "System.Media.SoundPlayer.Play("file.mp3");"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Mum();
        }
        void Mum()
        {
            System.Media.SoundPlayer.Play();
        }
    }
}
mathgenius
  • 503
  • 1
  • 6
  • 21
  • You need to actually create a `SoundPlayer`... `Play()` isn't a static method... Look at the [MDSN docs](https://msdn.microsoft.com/en-us/library/system.media.soundplayer%28v=vs.110%29.aspx) – Broots Waymb Sep 15 '15 at 14:13
  • I take it you have added the reference to that library in your project? –  Sep 15 '15 at 14:15
  • Furthermore your ressource should be passed in SoundPlayer constructor or by calling the Load(); method. You cant pass it to Play(); – Bgl86 Sep 15 '15 at 14:16
  • Also, from that link I posted, `SoundPlayer` is not able to play mp3's. "The SoundPlayer class cannot play other file types, such as .wma or .mp3. If you want to play other file types, you can use the Windows Media Player control." – Broots Waymb Sep 15 '15 at 14:17

1 Answers1

2

To play an mp3 file, You will need to add the windows media player Library. 1. Add a reference to the WMP library - in your solution explorer, under your project, go to References, right click and add a reference to WMP. It will be under the COM libraries, I think. 2. Add "using WMPLib;" to your code at the top, 3. Add this code to play the file:

        WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
        myplayer.URL = "mysound.mp3";
        myplayer.controls.play();

mind you the URL is the path to the file you want to play. If it has backslashes in it, you will have to "escape" them. Like this

 myplayer.URL = "C:\\Users\\Public\\Music\\Sample Music\\Kalimba.mp3";
  • The file I want to play is embedded in the project, when it should just be "file.mp3", right? – mathgenius Sep 15 '15 at 18:54
  • Give it a shot. Basically, that has to be the url, which for windows or console applications is the path to the file. For web apps, it can be a relative url. If you are doing a console app, then a relative path, like just the file name, might be correct if the file is in the same folder as the app. –  Sep 15 '15 at 19:20
  • Thanks a lot for your detailed explanation, but now I realise I should have mentioned I tried that but not only do I not have a "WMPLibrary" under COM libraries or anywhere else, but typing "using WMPLib;" generates a "could not be found" error. – mathgenius Sep 18 '15 at 11:39
  • Ok, sorry about this. Under the COM libraries, look for "Windows Media Player". When you add a reference to that, the "using WMPLib;" should work. –  Sep 18 '15 at 13:21
  • Thanks! That solved almost anything. The command you gave me now builds correctly, the program starts, the console appears but no sound is played. I have tried referencing the file both as "file.mp3", since it's embedded and as "C:\\file.mp3" but nothing happens. I have copied your code exactly. – mathgenius Sep 21 '15 at 08:02
  • .URL = "file.mp3" plays the IMMEDIATELY, and then controls.play() plays it AGAIN ! This is in debug mode, but I have to put a breakpoint, if I If I run it without a breakpoint, it doesn't play ! – joedotnot Sep 25 '22 at 05:27