您的位置:首页 > 编程语言 > C#

C#调用winmm.dll播放声音_中国创造

2007-12-15 16:09 627 查看
C#调用winmm.dll播放声音2006-08-12 08:52下面的 C# 代码示例演示如何在 Windows 平台上使用平台调用服务来播放波形声音文件。
示例
此代码示例使用 DllImport 将 winmm.dll 的 PlaySound 方法入口点作为 Form1 PlaySound() 导入。该示例具有一个带有一个按钮的简单 Windows 窗体。单击该按钮可打开标准 Windows OpenFileDialog 对话框,以便打开要播放的文件。选择波形文件后,将使用 winmm.DLL 程序集方法的 PlaySound() 方法来播放此文件。有关 winmm.dll 的 PlaySound 方法的更多信息,请参见 Using PlaySound to Play Waveform-Audio Files(使用 PlaySound 播放波形音频文件)。浏览并选择一个带 .wav 扩展名的文件,然后单击“打开”来使用平台调用播放该波形文件。将在文本框中显示选择的文件的完整路径。
“打开文件”对话框会进行筛选,以便只显示具有 .wav 扩展名的文件,其筛选器设置为:
C# 复制到剪贴板
dialog1.Filter = "Wav Files (*.wav)|*.wav";

C# 复制到剪贴板
using System.Windows.Forms;

namespace WinSound
{
public partial class Form1 : Form
{
private TextBox textBox1;
private Button button1;

public Form1() //constructor
{
InitializeComponent();
}

[System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

[System.Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}

private void button1_Click (object sender, System.EventArgs e)
{
OpenFileDialog dialog1 = new OpenFileDialog();

dialog1.Title = "Browse to find sound file to play";
dialog1.InitialDirectory = @"c:/";
dialog1.Filter = "Wav Files (*.wav)|*.wav";
dialog1.FilterIndex = 2;
dialog1.RestoreDirectory = true;

if(dialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = dialog1.FileName;
PlaySound (dialog1.FileName, new System.IntPtr(), PlaySoundFlags.SND_SYNC);
}
}
}
}

3编译代码
3编译代码
在 Visual Studio 中创建一个新的 C# Windows 应用程序项目,并命名为 WinSound。
复制上面的代码并将其粘贴到 Form1.cs 文件中以覆盖原来的内容。
复制下面的代码,并将它粘贴到 Form1.Designer.cs 文件的 InitializeComponent() 方法中现有代码的后面。
C# 复制到剪贴板
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Browse";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "FIle path";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Platform Invoke WinSound C#";
this.ResumeLayout(false);
this.PerformLayout();

4.编译并运行代码。

本文转自
http://hi.baidu.com/%D6%D0%B9%FA%B4%B4%D4%EC/blog/item/b8089a50335bc462853524ad.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: