如何从资源中播放WAV音频文件?

47

我该如何在我的项目资源中播放WAV音频文件?我的项目是一个使用C#编写的Windows窗体应用程序。


这就是你要找的内容:http://msdn.microsoft.com/en-us/library/3w5b27z4.aspx - Liviu Mandras
7个回答

60

由于mySoundFile是一个Stream对象,您可以利用SoundPlayer的重载构造函数,该构造函数接受一个Stream对象:

System.IO.Stream str = Properties.Resources.mySoundFile;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
snd.Play();

SoundPlayer类文档(MSDN)


3
这段代码在Windows CE中会抛出异常,因为它不会自动将资源从byte[]转换为stream。我发现以下代码能够解决该问题,并将其留在这里供其他人参考:https://dev59.com/pkrSa4cB1Zd3GeqPZdCC - Hagelt18
我们实际上不需要声明一个单独的Stream变量 ;) - TomeeNS
4
当然可以,但它展示了人们所使用的SoundPlayer构造函数的类型和负载。 - Evan Mulawski
1
此答案比标记的答案更好,因为您不需要可能已过时的额外类。同时,作者还打出了命名空间。 - René W.

52

a) 好的,首先将音频文件(.wav)添加到项目资源中。

  1. 从菜单工具栏(“VIEW”)打开“Solution Explorer”,或者直接按Ctrl + Alt + L。
  2. 单击“Properties”的下拉列表。
  3. 然后选择“Resource.resx”并按Enter键。

open project resource

  1. 现在从组合框列表中选择“Audio”。

add audio files to resource

  1. 然后单击“Add Resource”,选择音频文件(.wav)并单击“打开”。

browsing for audio files

  1. 选择音频文件并将“Persistence”属性更改为“嵌入式 .resx”。

embedding audio files to resource

b) 现在,只需编写以下代码来播放音频。

在此代码中,我在窗体加载事件上播放音频。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Media; // at first you've to import this package to access SoundPlayer

namespace WindowsFormsApplication1
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        private void login_Load(object sender, EventArgs e)
        {
            playaudio(); // calling the function
        }

        private void playaudio() // defining the function
        {
            SoundPlayer audio = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.Connect); // here WindowsFormsApplication1 is the namespace and Connect is the audio file name
            audio.Play();
        }
    }
}

就这样了。
一切都完成了,现在运行项目(按f5键),享受你的声音。
祝一切顺利。 :)


1
正是我想要的!这些图片帮了我很多。你知道我是否也可以用这种方式嵌入字体(比如fontawesome)以供我的程序使用吗? - Xander Luciano
当我添加一个wav文件时,它会添加一个Resources2.Designer.cs,这似乎是Resources.Designer.cs的副本 - 这导致冲突。这种情况刚刚开始发生。有什么想法可能正在发生?https://www.screencast.com/t/xAVpE5v6b0 - Matt

11

2
你需要小心垃圾回收器在声音仍在播放时释放其使用的内存。虽然很少发生,但一旦发生,你将只听到一些随机的声音。有一个解决方案,包括实现你想要的源代码,你可以在这里找到:http://msdn.microsoft.com/en-us/library/dd743680(VS.85).aspx。请滚动到底部,在“社区内容”部分查找。

1

Theses two lines can do it:

SoundPlayer sound = new SoundPlayer(Properties.Resources.solo);     
sound.Play(); 

0

当您需要将声音添加到项目中时,可以通过播放.wav文件来实现。然后您需要像这样添加.wav文件。

using System.Media; //write this at the top of the code

SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");
my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish

请记住,您必须使用正斜杠/)格式编写文件路径,不要在给文件路径时使用反斜杠\),否则会出现错误。

另外,请注意,如果您希望在播放声音时发生其他事情,可以将my_wave_file.PlaySync();更改为my_wave_file.PlayAsync();


0

据我所知,有两种方法可以实现,如下所示:

  1. 使用文件路径

首先将文件放置在项目的根目录中,然后无论您在 DebugRelease 模式下运行程序,该文件都可以被访问。然后使用 SoundPlayer 类来播放它。
但是,这种方式需要将声音文件与其文件夹层次结构复制到除了 "bin" 目录下的 "Release" 文件夹层次结构之外的所有文件夹层次结构中,如果您想将项目发布给用户。

var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
SoundPlayer player = new SoundPlayer();
player.SoundLocation = Path.Combine(basePath, @"./../../Reminder.wav");
player.Load();
player.Play();
  1. 使用资源

按照下面的动画,将“现有文件”添加到项目中。

enter image description here

SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
player.Play();

这种方法的优点是:
只需要复制“bin”目录下的“Release”文件夹即可运行程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接