2015-12-06 55 views
0

(2015-12-06) 我使用Visual Studio 2013 C#,我想知道如何讓程序播放MP3文件作爲背景音樂,即使它分發給別人的電腦(而不僅僅是我的電腦上有我自己的MP3文件路徑 - 例如c:\my documents\so and so music.mp3)。播放MP3文件,即使分發

(2015-12-07) 感謝Maximilian Gerhardt的回覆。但是當我用我自己的out.wav嘗試時,我被卡住了幾條紅線......(編譯錯誤..)我不太確定如何繼續。 (對不起,我是C#的新手) This is what I see - the image of the compile error screen

回答

0

首先在項目的某處創建一個新的資源文件。使用Add -> New element上下文菜單中的項目資源管理器中做到這一點:(是的,對不起,這是在德國) enter image description here

一旦你創建你的資源文件,你可以在它的項目資源管理器中雙擊並添加資源到它。我添加了一個名爲out.wav.wav文件。這可能是您的.mp3文件。 abc 接下來,您只需在代碼中訪問該流。添加資源文件也會生成一些代碼,讓您以流的形式訪問該文件。用法例如爲:

using System; 
using System.IO; 
using System.Text; 

namespace ResourceTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //Create a memory stream to read the file into.. 
      MemoryStream outStream = new MemoryStream(); 
      //Copy our _out.wav File 
      SomeResource._out.CopyTo(outStream); 
      //Also possible: 
      //SomeResource.ResourceManager.GetStream("out.wav")... 
      //Read into a byte array. 
      byte[] file_bytes = outStream.ToArray(); 
      //Close it 
      outStream.Close(); 

      //Do something with file_bytes.. 
     } 
    } 
} 

現在你已經嵌入你的ressource中的ressource文件,它總是訪問(即資源總是得到你所編譯的二進制複製)

+0

感謝您的幫助。但不幸的是......無法讓它工作..(請看附件) –

+0

類名「SomeResource」是從我創建的資源文件的名稱('SomeResource.resx')派生而來的。您必須將類名更改爲您創建的資源文件的文件名。 (有'Resources.resx',所以你應該試試'Resources._out'?) –

+0

啊哈。就這樣。我重新編寫了代碼,編譯時沒有錯誤。但是,然後......它也沒有播放任何聲音。它看起來像我們設法將資源加載到內存中,但我們如何從內存中播放資源......?對於提出太多問題抱歉。 –