【发布时间】:2012-04-14 03:44:06
【问题描述】:
我在网上找到了一些代码片段,但问题是它们使用系统上本地托管的文件位置,我想从我的应用程序中播放 MP3 或 wav 并将 mp3 文件构建到应用程序中,而不是他们也需要它作为.exe。知道如何做这样的事情吗?
【问题讨论】:
-
WPF 还是 WinForms,什么样的应用?
我在网上找到了一些代码片段,但问题是它们使用系统上本地托管的文件位置,我想从我的应用程序中播放 MP3 或 wav 并将 mp3 文件构建到应用程序中,而不是他们也需要它作为.exe。知道如何做这样的事情吗?
【问题讨论】:
嵌入您的 mp3 或 wav 作为资源:
使用以下代码播放:
WindowsMediaPlayer wmp = new WindowsMediaPlayer();
Stream stream = Assembly.GetExecutingAssembly(). GetManifestResourceStream("yourfile.mp3");
string temppath = Path.GetTempPath() + "\\temp.mp3";
using (Stream output = new FileStream (temppath, FileMode.Create))
{
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ( (read= stream.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
wmp.URL = temppath;
wmp.controls.play();
完成后不要忘记删除临时文件。
【讨论】: