简体   繁体   English

C#Mediaplayer无法从资源播放mp3文件

[英]C# Mediaplayer doesn't play mp3 file from resources

Mediaplayer didn't work for me, so I moved to a simple test project (C# Console App). Mediaplayer对我不起作用,因此我转到了一个简单的测试项目(C#控制台应用程序)。 I added my .mp3 file to the project like this: 我将我的.mp3文件添加到项目中,如下所示:

  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 添加我的warn.mp3文件
  5. left click the warn.mp3 file 左键单击warn.mp3文件
  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? 有什么不对的想法吗?

You cannot use MediaPlayer from a internal exe/dll resource. 您不能从内部exe / dll资源使用MediaPlayer You should choose another player component or write it to disk. 您应该选择另一个播放器组件或将其写入磁盘。 If you can choose another player, looks like System.Media.SoundPlayer could do the trick. 如果您可以选择其他播放器,则System.Media.SoundPlayer可以解决问题。 Search for stack overflow Play wav/mp3 from memory there should give some results 搜索堆栈溢出Play wav/mp3 from memory应该会给出一些结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM