2017-06-05 249 views
-1

Mediaplayer沒有爲我工作,所以我轉移到了一個簡單的測試項目(C#控制檯應用程序)。我將我的MP3檔案,該項目是這樣的:在解決方案資源管理C#Mediaplayer不會播放來自資源的mp3文件

  1. 右擊項目名稱(測試)
  2. 添加文件夾資源
  3. 右鍵單擊Solution Explorer中的資源文件夾
  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(); 
     } 
    } 
} 

什麼是錯的任何想法?

+0

Dupplicated,btw MediaPlayer不允許嵌入資源https://stackoverflow.com/questions/3728181/how-would-i-use-a-pack-uri-resource-與媒體播放器 – bradbury9

回答

0

您無法從內部EXE/DLL資源使用MediaPlayer。您應該選擇其他播放器組件或將其寫入磁盤。如果你可以選擇另一個球員,看起來像System.Media.SoundPlayer可以做到這一點。搜索堆棧溢出Play wav/mp3 from memory應該有一些結果

0

這個怎麼樣:

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

那麼,這並沒有幫助。我不想從程序中包含外部聲音文件,我希望它被「打包」,就像我以相同的方式添加圖片文件一樣,然後我可以在Image控件中顯示它,而無需讓電腦上的文件不再(在另一臺機器上編譯後) –

+0

然後將其添加到您的項目中。右鍵單擊添加文件並添加它。它會被複制到你的bin文件夾中。 – NicoRiff

+0

如果我說得對,「包裝」意味着嵌入式資源。 AFAIK MediaPlayer不允許這樣做。 – bradbury9