zoukankan      html  css  js  c++  java
  • 用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   }
  • 相关阅读:
    查找链表中是否有环linked-list-cycle
    reverse-integer
    AVL树之 Java的实现
    single-number
    Best Time to Buy and Sell Stock II
    maximun-depth-of-binary-tree
    minimun-depth-of-binary-tree
    剑指offer--矩阵中的路径
    grep的几个参数
    fsck和badlocks
  • 原文地址:https://www.cnblogs.com/qq260250932/p/4236649.html
Copyright © 2011-2022 走看看