播放声音

C#中没有声音类。
播放声音请参阅API!
比如:

[DllImport("Winmm.dll")]
public static extern long PlaySound(string name,long  module,long flag);

播放声音

using System;
using System.Runtime.InteropServices;

class musicplay
{
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long length, long hwndcallback);
    public musicplay(string name)
    {
        filename = name;
    }
    public void play()
    {
        t = mciSendString(@"open " + filename, m, 0, 0);
        t = mciSendString(@"play " + filename + @" wait", m, 0, 0);
        t = mciSendString(@"close " + filename, m, 0, 0);
    }
    private string filename;
    private string m = @"                                  ";
    private long t;
}

class test
{
    static void Main()
    {
        Console.WriteLine("请输入你要播放的歌曲的路径");
        musicplay mpl = new musicplay(Console.ReadLine());
        mpl.play();
        Console.WriteLine("播放完毕,再见");

    }
}

================================================================================================

public class Form1 : System.Windows.Forms.Form
{
    double shu;
    double temps1;
    double temps2;
    double jg;
    double save;
    double zf;
    string js;
    bool gl;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.Button button5;
    private System.Windows.Forms.Button button6;
    private System.Windows.Forms.Button button7;
    private System.Windows.Forms.Button button8;
    private System.Windows.Forms.Button button9;
    private System.Windows.Forms.Button button10;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button11;
    private System.Windows.Forms.Button button12;
    private System.Windows.Forms.Button button13;
    private System.Windows.Forms.Button button14;
    private System.Windows.Forms.Button button15;
    private System.Windows.Forms.Button button16;
    private System.Windows.Forms.Button button17;
    private System.Windows.Forms.Button button18;
    private System.Windows.Forms.Button button19;
    private System.Windows.Forms.Button button20;
    private System.Windows.Forms.Button button21;
    private System.Windows.Forms.Button button22;
    private System.Windows.Forms.Button button23;

    [DllImport("winmm.dll", EntryPoint = "PlaySound")]
    public static extern bool PlaySound(ref Byte snd, IntPtr hmod, uint fdwSound);

    [DllImport("winmm.dll", EntryPoint = "PlaySound")]
    public static extern int PlaySound(string lpszName, int hModule, int dwFlags);

    public enum PlayingFlags : uint
    {
        SND_SYNC = 0x00,
        SND_ASYNC = 0x01,
        SND_NODEFAULT = 0x02,
        SND_MEMORY = 0x04,
        SND_ALIAS = 0x010000,
        SND_FILENAME = 0x020000,
        SND_RESOURCE = 0x040004,
        SND_ALIAS_ID = 0x0110000,
        SND_ALIAS_START = 0,
        SND_LOOP = 0x08,
        SND_NOSTOP = 0x010,
        SND_VALID = 0x01F,
        SND_NOWAIT = 0x02000,
        SND_PURGE = 0x40
    }
//...................................................省略
    private void button1_Click(object sender, System.EventArgs e)
    {
        Form1.PlaySound(filename.wav, 0, (int)Form1.PlayingFlags.SND_SYNC);
        Button btn = (Button)sender;
        if (gl)
        {
            textBox1.Text = btn.Text;
            gl = false;
        }
        else if (textBox1.Text == "0")
        {
            textBox1.Text = btn.Text;
            gl = false;
        }
        else
        {
            textBox1.Text = textBox1.Text + btn.Text;
            gl = false;
        }
        shu = Convert.ToDouble(textBox1.Text);
    }
Contributors: FHL