2012-07-23 38 views
0

我正在制作一个程序,该程序在启动时会播放一些音乐,并且很容易。我要的是有音乐文件保存为解决方案的一部分,让音乐永远玩,而不是指向那里的音乐保存在程序:如何在应用程序中存储文件

Dim wavMusic As String = "\\My Documents\Visual Studio 2010\Song.wav" 

所以,如果我例如,将应用程序移动到其他计算机上,它将无法找到歌曲。所以我想把这首歌作为.exe的一部分,这样它就可以随时找到它。谢谢。

哦,我使用的是vb和winforms,因为我在学校做这个,在C和WPF这些令人愉快的东西没有被使用。

回答

3

将它放入您的项目资源中,可作为项目属性中的选项卡访问。您可以使用属性访问所有资源。所以:

  • 我的项目在Solution Explorer
  • 转到双击该资源标签
  • 将您的文件转换成白色的大空间
  • 在你的代码,访问它as My.Resources.Song

Voilà!

0

您可以嵌入在资源文件中的音频文件作为资源:

enter image description here

enter image description here

enter image description here

private void btnDemo2_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SoundPlayer sndplayr = new 
      SoundPlayer(PlayWavFiles.Properties.Resources.LoopyMusic); 
     if (btnDemo2.Text == "Demo WAV 2") 
     { 
      sndplayr.PlayLooping(); 
      btnDemo2.Text = "STOP"; 
     } 
     else 
     { 
      sndplayr.Stop(); 
      btnDemo2.Text = "Demo WAV 2"; 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message + ": " + ex.StackTrace.ToString(), 
      "Error"); 
    } 
} 

请参阅这篇文章:http://www.codeproject.com/Articles/17422/Embedding-and-Playing-WAV-Audio-Files-in-a-WinForm

相关问题