C# Mediaplayer doesn't play mp3 file from resources

会有一股神秘感。 提交于 2019-12-12 05:13:50

问题


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?


回答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




回答2:


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();
        }
    }
}


来源:https://stackoverflow.com/questions/44369356/c-sharp-mediaplayer-doesnt-play-mp3-file-from-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!