C# 多種方式播放Wav聲音

來源:互聯網
上載者:User
代碼

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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.