2015-12-06 57 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

啊哈。就这样。我重新编写了代码,编译时没有错误。但是,然后......它也没有播放任何声音。它看起来像我们设法将资源加载到内存中,但我们如何从内存中播放资源......?对于提出太多问题抱歉。 –