0

Mediaplayer didn't work for me, so I moved to a simple test project (C# Console App). I added my .mp3 file to the project like this:

  1. Right click project name (test) in solution explorer
  2. add folder resources
  3. Right click the resources folder in solution explorer
  4. Add my warn.mp3 file
  5. left click the warn.mp3 file
  6. Changed Build Action to Resource in the properties window.

Sadly, this code doesnt work:

namespace test
{
    class Program
    {
        public static void Main(string[] args)
        {
            MediaPlayer player = new MediaPlayer();
            player.Open(new Uri("resources/warn.mp3", UriKind.Relative));
            player.Play();
            Console.ReadKey();
        }
    }
}

However, this one does:

namespace test
{
    class Program
    {
        public static void Main(string[] args)
        {
            MediaPlayer player = new MediaPlayer();
            player.Open(new Uri("C:\\Users\\Krepsy3\\Documents\\Programs\\OOP\\test\\test\\resources\\warn.mp3", UriKind.Absolute));
            player.Play();
            Console.ReadKey();
        }
    }
}

Any idea about what is wrong?

Krepsy 3
  • 63
  • 1
  • 2
  • 11
  • Dupplicated, btw MediaPlayer does not allow embeded resources https://stackoverflow.com/questions/3728181/how-would-i-use-a-pack-uri-resource-with-media-player – Cleptus Jun 05 '17 at 13:17

2 Answers2

1

You cannot use MediaPlayer from a internal exe/dll resource. You should choose another player component or write it to disk. If you can choose another player, looks like System.Media.SoundPlayercould do the trick. Search for stack overflow Play wav/mp3 from memory there should give some results

Cleptus
  • 3,446
  • 4
  • 28
  • 34
-1

What about this:

namespace test
{
    class Program
    {
        public static void Main(string[] args)
        {
            MediaPlayer player = new MediaPlayer();
            player.Open(new Uri(System.Environment.CurrentDirectory + "\resources\warn.mp3", UriKind.Relative));
            player.Play();
            Console.ReadKey();
        }
    }
}
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • 1
    Well that doesn't help. I don't want to include the sound file extrnally from the program, I want it to be "packed in", like if I added a picture file the same way, I am then able to display it in the Image control, without having to have the file on the computer anymore (after compilation on f.e. another machine) – Krepsy 3 Jun 05 '17 at 13:04
  • Then add it to your project. Right Click Add File and add it. It will be copied to your bin folder. – NicoRiff Jun 05 '17 at 13:05
  • If i get it right, "packed in" means embedded resource. AFAIK MediaPlayer does not allow that. – Cleptus Jun 05 '17 at 13:19
  • As I said, I did exactly that: add it to my project. @bradbury9 Thx for explanation. Is there a way in C# to play .mp3 file which is an embedded resource? – Krepsy 3 Jun 05 '17 at 15:00
  • @Krepsy3 You can do it using NAudio API. See https://github.com/naudio/NAudio – help Jan 07 '21 at 01:51