網頁

2011年10月17日 星期一

[C#] 播放wav


如何:在 Windows 窗体上播放声音



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

using System.Media; // For SoundPlayer

namespace wavplay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\notify.wav");
            simpleSound.Play();
        }
    }
}

如何:从 Windows 窗体播放资源中嵌入的声音


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

using System.Media; // For SoundPlayer

namespace wavplay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream s = a.GetManifestResourceStream(".chimes.wav");
            SoundPlayer player = new SoundPlayer(s);
            player.Play();
        }
    }
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。