用C#实现多种方式播放Wav声音

  1  
  3  
  4 
  5   using System;
  6 
  7   using System.Collections.Generic;
  8 
  9   using System.ComponentModel;
 10 
 11   using System.Data;
 12 
 13   using System.Drawing;
 14 
 15   using System.Text;
 16 
 17   using System.Windows.Forms;
 18 
 19   using System.Media;
 20 
 21   using System.Resources;
 22 
 23   using System.IO;
 24 
 25   namespace SoundPlayerApp
 26 
 27   {
 28 
 29   public partial class Form1 : Form
 30 
 31   {
 32 
 33   private SoundPlayer simpleSound;
 34 
 35   public Form1()
 36 
 37   {
 38 
 39   InitializeComponent();
 40 
 41   }
 42 
 43   private void button1_Click(object sender, EventArgs e)
 44 
 45   {
 46 
 47   OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
 48 
 49   OpenFileDialog1.Filter = "Wav 文件(*.wav)|*.wav";
 50 
 51   if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
 52 
 53   {
 54 
 55   simpleSound = new SoundPlayer(OpenFileDialog1.FileName);
 56 
 57   simpleSound.Play();
 58 
 59   }
 60 
 61   }
 62 
 63   private void button2_Click(object sender, EventArgs e)
 64 
 65   {
 66 
 67   OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
 68 
 69   OpenFileDialog1.Filter = "Wav 文件(*.wav)|*.wav";
 70 
 71   if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
 72 
 73   {
 74 
 75   simpleSound = new SoundPlayer(OpenFileDialog1.FileName);
 76 
 77   simpleSound.PlayLooping();
 78 
 79   }
 80 
 81   }
 82 
 83   private void button3_Click(object sender, EventArgs e)
 84 
 85   {
 86 
 87   if (simpleSound != null) simpleSound.Stop();
 88 
 89   }
 90 
 91   private void button4_Click(object sender, EventArgs e)
 92 
 93   {
 94 
 95   simpleSound = new SoundPlayer(Properties.Resources.big);
 96 
 97   simpleSound.Play();
 98 
 99   }
100 
101    
102   private void button5_Click(object sender, EventArgs e)
103 
104   {
105 
106   simpleSound = new SoundPlayer(Properties.Resources.big);
107 
108   simpleSound.PlayLooping();
109 
110   }
111 
112   private void button6_Click(object sender, EventArgs e)
113 
114   {
115 
116   if (simpleSound != null) simpleSound.Stop();
117 
118   }
119 
120   private void button7_Click(object sender, EventArgs e)
121 
122   {
123 
124   switch (comboBox1.Text)
125 
126   {
127 
128   case "星号(错误)":
129 
130   SystemSounds.Asterisk.Play();
131 
132   break;
133 
134   case "默认响声(叮当声)":
135 
136   SystemSounds.Beep.Play();
137 
138   break;
139 
140   case "感叹号(惊叹号)":
141 
142   SystemSounds.Exclamation.Play();
143 
144   break;
145 
146   case "关键性停止(关键性终止)":
147 
148   SystemSounds.Hand.Play();
149 
150   break;
151 
152   case "问题":
153 
154   SystemSounds.Question.Play();
155 
156   break;
157 
158   }
159 
160   }
161 
162   private void button8_Click(object sender, EventArgs e)
163 
164   {
165 
166   ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("SoundResource", Application.StartupPath, null);//资源文件不带扩展名称
167 
168   byte[] buffer = (byte[])rm.GetObject("Sound.wav");
169 
170   FileStream FS = new FileStream("Sound.wav", FileMode.Create);//新建文件
171 
172   BinaryWriter BWriter = new BinaryWriter(FS);//以二进制打开文件流
173 
174   BWriter.Write(buffer, 0, buffer.Length);//从资源文件读取声音文件内容,写入到一个声音文件中
175 
176   BWriter.Close();
177 
178   FS.Close();
179 
180   simpleSound = new SoundPlayer("Sound.wav");
181 
182   simpleSound.Play();
183 
184   }
185 
186   private void button9_Click(object sender, EventArgs e)
187 
188   {
189 
190   ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("SoundResource", Application.StartupPath, null);//资源文件不带扩展名称
191 
192   byte[] buffer = (byte[])rm.GetObject("Sound.wav");
193 
194   FileStream FS = new FileStream("Sound.wav", FileMode.Create);//新建文件
195 
196   BinaryWriter BWriter = new BinaryWriter(FS);//以二进制打开文件流
197 
198   BWriter.Write(buffer, 0, buffer.Length);//从资源文件读取声音文件内容,写入到一个声音文件中
199 
200   BWriter.Close();
201 
202   FS.Close();
203 
204   simpleSound = new SoundPlayer("Sound.wav");
205 
206   simpleSound.PlayLooping();
207 
208   }
209 
210   private void button10_Click(object sender, EventArgs e)
211 
212   {
213 
214   if (simpleSound != null) simpleSound.Stop();
215 
216   }
217 
218   }
219 
220   }

 

转载于:https://www.cnblogs.com/qq260250932/p/4236649.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用NAudio播放WAV文件非常简单,你可以按照以下步骤来实现: 1. 首先,你需要从NAudio的官方网站下载并安装NAudio库,或者使用NuGet包管理器安装NAudio。 2. 在C# WinForm应用程序中,添加一个“打开文件”按钮和一个“播放”按钮,并在按钮的单击事件中编写代码。 3. 在打开文件按钮的单击事件中,打开文件对话框并选择要播放WAV文件,并将文件名存储在一个字符串变量中。 4. 在播放按钮的单击事件中,创建一个WaveFileReader对象,用于读取WAV文件的数据。然后,创建一个WaveOut对象,用于播放WAV文件的数据。最后,调用WaveOut对象的Play方法,开始播放WAV文件。 以下是示例代码: ``` using NAudio.Wave; private string fileName; private void btnOpenFile_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "WAV files (*.wav)|*.wav"; if (openFileDialog.ShowDialog() == DialogResult.OK) { fileName = openFileDialog.FileName; } } private void btnPlay_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(fileName)) { WaveFileReader waveFileReader = new WaveFileReader(fileName); WaveOut waveOut = new WaveOut(); waveOut.Init(waveFileReader); waveOut.Play(); } } ``` 在上面的代码中,我们首先在btnOpenFile_Click事件中打开文件对话框并选择要播放WAV文件,并将文件名存储在fileName变量中。然后,在btnPlay_Click事件中,我们创建一个WaveFileReader对象,用于读取WAV文件的数据。然后,我们创建一个WaveOut对象,并调用其Init方法,将WaveFileReader对象传递给它。最后,我们调用WaveOut对象的Play方法,开始播放WAV文件。 请注意,以上代码只是一个简单的示例,你可能需要根据自己的需求进行适当的修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值