C# 多种方式播放Wav声音

ContractedBlock.gif ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

using  System.Media;
using  System.Resources;
using  System.IO;

namespace  SoundPlayerApp
{
    
public   partial   class  Form1 : Form
    {
        
private  SoundPlayer simpleSound;
        
public  Form1()
        {
            InitializeComponent();
        }
        
private   void  button1_Click( object  sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog1 
=   new  OpenFileDialog();
            OpenFileDialog1.Filter 
=   " Wav 文件(*.wav)|*.wav " ;
            
if  (OpenFileDialog1.ShowDialog()  ==  DialogResult.OK)
            {
                simpleSound 
=   new  SoundPlayer(OpenFileDialog1.FileName);
                simpleSound.Play();
            }
        }

        
private   void  button2_Click( object  sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog1 
=   new  OpenFileDialog();
            OpenFileDialog1.Filter 
=   " Wav 文件(*.wav)|*.wav " ;
            
if  (OpenFileDialog1.ShowDialog()  ==  DialogResult.OK)
            {
                simpleSound 
=   new  SoundPlayer(OpenFileDialog1.FileName);
                simpleSound.PlayLooping();
            }

        }

        
private   void  button3_Click( object  sender, EventArgs e)
        {
            
if  (simpleSound  !=   null ) simpleSound.Stop();
        }

        
private   void  button4_Click( object  sender, EventArgs e)
        {
            simpleSound 
=   new  SoundPlayer(Properties.Resources.big);
            simpleSound.Play();
        }

        
private   void  button5_Click( object  sender, EventArgs e)
        {
            simpleSound 
=   new  SoundPlayer(Properties.Resources.big);
            simpleSound.PlayLooping();
        }

        
private   void  button6_Click( object  sender, EventArgs e)
        {
            
if  (simpleSound  !=   null ) simpleSound.Stop();
        }

        
private   void  button7_Click( object  sender, EventArgs e)
        {
            
switch  (comboBox1.Text)
            {
                
case   " 星号(错误) " :
                    SystemSounds.Asterisk.Play();
                    
break ;
                
case   " 默认响声(叮当声) " :
                    SystemSounds.Beep.Play();
                    
break ;
                
case   " 感叹号(惊叹号) " :
                    SystemSounds.Exclamation.Play();
                    
break ;
                
case   " 关键性停止(关键性终止) " :
                    SystemSounds.Hand.Play();
                    
break ;
                
case   " 问题 " :
                    SystemSounds.Question.Play();
                    
break ;
            }
        }

        
private   void  button8_Click( object  sender, EventArgs e)
        {
            ResourceManager rm 
=  ResourceManager.CreateFileBasedResourceManager( " SoundResource " , Application.StartupPath,  null ); // 资源文件不带扩展名称
             byte [] buffer  =  ( byte [])rm.GetObject( " Sound.wav " );
            FileStream FS 
=   new  FileStream( " Sound.wav " , FileMode.Create); // 新建文件
            BinaryWriter BWriter  =   new  BinaryWriter(FS); // 以二进制打开文件流
            BWriter.Write(buffer,  0 , buffer.Length); // 从资源文件读取声音文件内容,写入到一个声音文件中
            BWriter.Close();
            FS.Close();
            simpleSound 
=   new  SoundPlayer( " Sound.wav " );
            simpleSound.Play();
        }

        
private   void  button9_Click( object  sender, EventArgs e)
        {
            ResourceManager rm 
=  ResourceManager.CreateFileBasedResourceManager( " SoundResource " , Application.StartupPath,  null ); // 资源文件不带扩展名称
             byte [] buffer  =  ( byte [])rm.GetObject( " Sound.wav " );
            FileStream FS 
=   new  FileStream( " Sound.wav " , FileMode.Create); // 新建文件
            BinaryWriter BWriter  =   new  BinaryWriter(FS); // 以二进制打开文件流
            BWriter.Write(buffer,  0 , buffer.Length); // 从资源文件读取声音文件内容,写入到一个声音文件中
            BWriter.Close();
            FS.Close();
            simpleSound 
=   new  SoundPlayer( " Sound.wav " );
            simpleSound.PlayLooping();
        }

        
private   void  button10_Click( object  sender, EventArgs e)
        {
            
if  (simpleSound  !=   null ) simpleSound.Stop();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值