简体   繁体   English

从应用程序播放MP3文件

[英]Play MP3 file from app

I've found some code snips online but issue is they use file locations hosted local on system, I would like to play a MP3 or wav from my app and build the mp3 file into the app rather then them needing it as well as the .exe. 我在网上找到了一些代码片段,但问题是它们使用系统本地托管的文件位置,我想从我的应用程序播放MP3或WAV文件并将mp3文件构建到应用程序中,而不是他们需要它以及。可执行程序。 Any idea of how to do such a thing? 关于如何做这样的事情的任何想法?

Embed your mp3 or wav as resource: 嵌入mp3或wav作为资源:

  1. Right click your project in solution explorer. 在解决方案资源管理器中右键单击您的项目。
  2. Select Add / Existing Item... from the menu Select and add your file. 从菜单中选择添加/现有项目...,然后添加文件。
  3. Select a newly added file in the solution explorer, right click on it 在解决方案资源管理器中选择一个新添加的文件,右键单击它
  4. Choose Properties from right-click menu 从右键菜单中选择属性
  5. Change its Build Action property to Embedded Resource 将其构建操作属性更改为嵌入式资源

Play it using the following code: 使用以下代码播放它:

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();

Don't forget to delete the temporary file when you are done with it. 完成后,不要忘记删除临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM