C#Mediaplayer无法播放资源中的mp3文件 [英] C# Mediaplayer doesn't play mp3 file from resources
问题描述
Mediaplayer对我不起作用,所以我转到了一个简单的测试项目(C#控制台应用程序).我将.mp3文件添加到项目中,如下所示:
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:
- 在解决方案资源管理器中右键单击项目名称(测试)
- 添加文件夹资源
- 右键单击解决方案资源管理器中的资源文件夹
- 添加我的warn.mp3文件
- 左键单击warn.mp3文件
- 在属性窗口中将构建操作"更改为资源".
可悲的是,此代码不起作用:
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();
}
}
}
但是,这确实做到了:
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();
}
}
}
有什么问题的想法吗?
推荐答案
您不能从内部exe/dll资源使用MediaPlayer
.您应该选择另一个播放器组件或将其写入磁盘.如果您可以选择其他玩家,则System.Media.SoundPlayer
可以解决问题.搜索堆栈溢出Play wav/mp3 from memory
,应该给出一些结果
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.SoundPlayer
could do the trick. Search for stack overflow Play wav/mp3 from memory
there should give some results
这篇关于C#Mediaplayer无法播放资源中的mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!