簡體   English   中英

C#Mediaplayer無法從資源播放mp3文件

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

Mediaplayer對我不起作用,因此我轉到了一個簡單的測試項目(C#控制台應用程序)。 我將我的.mp3文件添加到項目中,如下所示:

  1. 在解決方案資源管理器中右鍵單擊項目名稱(測試)
  2. 添加文件夾資源
  3. 在解決方案資源管理器中右鍵單擊資源文件夾
  4. 添加我的warn.mp3文件
  5. 左鍵單擊warn.mp3文件
  6. 在屬性窗口中將“構建操作”更改為“資源”。

可悲的是,這段代碼不起作用:

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應該會給出一些結果

那這個呢:

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