Monday, December 15, 2008

How to play sound with .NET

Play different sound with .NET

You can make some noise in .NET application using VB/C# .NET code. There are multiple ways to do this, I am giving one of them; hope it's fulfill your requirement.

VB.NET

'You can add some more sound as per your requirement

Public Enum MessageBeepType

[Default] = -1

OK = &H0

[Error] = &H10

Question = &H20

Warning = &H30

Information = &H40

End Enum

' This allows you to 'Beep' the sounds declared in the MessageBeepType

<Runtime.InteropServices.DllImport("USER32.DLL", setlasterror:=True)> _

Public Shared Function MessageBeep(ByVal type As MessageBeepType) As Boolean

End Function

' Allowing you to specify the frequency and duration of the sound.
<Runtime.InteropServices.DllImport("KERNEL32.DLL")> _
Public Shared Function Beep(ByVal frequency As Integer, ByVal duration As Integer) As Boolean
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBeep(MessageBeepType.Default)

End Sub

C#.NET

Public Enum MessageBeepType

{

Default = -1,

OK = 0x0,

Error = 0x10,

Question = 0x20,

Warning = 0x30,

Information = 0x40

}

//This allows you to 'Beep' the sounds declared in the MessageBeepType

[Runtime.InteropServices.DllImport("USER32.DLL", setlasterror = true)]

public static bool MessageBeep(MessageBeepType type)

{

}

' Allowing you to specify the frequency and duration of the sound.

[Runtime.InteropServices.DllImport("KERNEL32.DLL")]
public static bool Beep(int frequency, int duration)

{

}

Button1_Click(object sender, System.EventArgs e)

{

MessageBeep(MessageBeepType.Default);

}

No comments: