// (c) Copyright Jason Clark 2003
using System;
using Wintellect.Interop.Sound;
class App{
public static void Main(){
// Produce an OK beep
Sound.MessageBeep(BeepTypes.Ok);
System.Threading.Thread.Sleep(1000);
// Ok, now for some circa-1977 si-fi
Random rand = new Random();
for( Int32 index = 0; index < 7 ; index++ ) {
Sound.Beep( rand.Next(500)+1000, TimeSpan.FromSeconds(.10));
}
// Pick a wave, any wave
Sound.PlayWave( @"D:\Program Files\Tencent\QQ\sound\folder.wav", true );
System.Threading.Thread.Sleep( 2000 );
Sound.StopWave();
}
}
namespace Wintellect.Interop.Sound{
using System.Runtime.InteropServices;
using System.ComponentModel;
sealed class Sound{
// Friendly MessageBeep() wrapper
public static void MessageBeep(BeepTypes type) {
if( !MessageBeep( (UInt32) type ) ) {
Int32 err = Marshal.GetLastWin32Error();
throw new Win32Exception( err );
}
}
// Friendly low-level beep wrapper
public static void Beep(Int32 frequency, Int32 milliseconds) {
if(!UnmanagedBeep( (UInt32)frequency, (UInt32)milliseconds ) ) {
Int32 err = Marshal.GetLastWin32Error();
throw new Win32Exception( err );
}
}
// Friendly low-level beep wrapper with TimeSpan
public static void Beep(Int32 frequency, TimeSpan duration) {
Beep( frequency, (Int32)duration.TotalMilliseconds );
}
// Friendly PlaySound() wrapper for playing wave files
public static void PlayWave(String filename, Boolean looped) {
UInt32 flags = sndAsyncFlag | sndFilenameFlag;
if( looped ) flags |= sndLoopFlag;
if( !PlaySound( filename, IntPtr.Zero, flags ) ) {
Int32 err = Marshal.GetLastWin32Error();
throw new Win32Exception( err );
}
}
// Friendly PlaySound() wrapper for playing wave files
public static void PlayWave(String filename){
PlayWave( filename, false );
}
// Friendly PlaySound() wrapper for stopping wave files
public static void StopWave(){
PlayWave( null );
}
// static extern methods for making sound through interop
[DllImport( "User32.dll", SetLastError = true )]
static extern Boolean MessageBeep(UInt32 beepType);
[DllImport( "Kernel32.dll", EntryPoint = "Beep", SetLastError = true)]
static extern Boolean UnmanagedBeep(UInt32 frequency, UInt32 duration);
[DllImport( "Winmm.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern Boolean PlaySound(String filename,
IntPtr module, UInt32 flags);
// Some private helper values for calling PlaySound
const UInt32 sndAsyncFlag = 0x0001;
const UInt32 sndLoopFlag = 0x0008;
const UInt32 sndFilenameFlag = 0x00020000;
private Sound(){}
}
// Enum for message beep types
enum BeepTypes{
Simple = -1,
Ok = 0x00000000,
IconHand = 0x00000010,
IconQuestion = 0x00000020,
IconExclamation = 0x00000030,
IconAsterisk = 0x00000040
}
}
using System;
using Wintellect.Interop.Sound;
class App{
public static void Main(){
// Produce an OK beep
Sound.MessageBeep(BeepTypes.Ok);
System.Threading.Thread.Sleep(1000);
// Ok, now for some circa-1977 si-fi
Random rand = new Random();
for( Int32 index = 0; index < 7 ; index++ ) {
Sound.Beep( rand.Next(500)+1000, TimeSpan.FromSeconds(.10));
}
// Pick a wave, any wave
Sound.PlayWave( @"D:\Program Files\Tencent\QQ\sound\folder.wav", true );
System.Threading.Thread.Sleep( 2000 );
Sound.StopWave();
}
}
namespace Wintellect.Interop.Sound{
using System.Runtime.InteropServices;
using System.ComponentModel;
sealed class Sound{
// Friendly MessageBeep() wrapper
public static void MessageBeep(BeepTypes type) {
if( !MessageBeep( (UInt32) type ) ) {
Int32 err = Marshal.GetLastWin32Error();
throw new Win32Exception( err );
}
}
// Friendly low-level beep wrapper
public static void Beep(Int32 frequency, Int32 milliseconds) {
if(!UnmanagedBeep( (UInt32)frequency, (UInt32)milliseconds ) ) {
Int32 err = Marshal.GetLastWin32Error();
throw new Win32Exception( err );
}
}
// Friendly low-level beep wrapper with TimeSpan
public static void Beep(Int32 frequency, TimeSpan duration) {
Beep( frequency, (Int32)duration.TotalMilliseconds );
}
// Friendly PlaySound() wrapper for playing wave files
public static void PlayWave(String filename, Boolean looped) {
UInt32 flags = sndAsyncFlag | sndFilenameFlag;
if( looped ) flags |= sndLoopFlag;
if( !PlaySound( filename, IntPtr.Zero, flags ) ) {
Int32 err = Marshal.GetLastWin32Error();
throw new Win32Exception( err );
}
}
// Friendly PlaySound() wrapper for playing wave files
public static void PlayWave(String filename){
PlayWave( filename, false );
}
// Friendly PlaySound() wrapper for stopping wave files
public static void StopWave(){
PlayWave( null );
}
// static extern methods for making sound through interop
[DllImport( "User32.dll", SetLastError = true )]
static extern Boolean MessageBeep(UInt32 beepType);
[DllImport( "Kernel32.dll", EntryPoint = "Beep", SetLastError = true)]
static extern Boolean UnmanagedBeep(UInt32 frequency, UInt32 duration);
[DllImport( "Winmm.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern Boolean PlaySound(String filename,
IntPtr module, UInt32 flags);
// Some private helper values for calling PlaySound
const UInt32 sndAsyncFlag = 0x0001;
const UInt32 sndLoopFlag = 0x0008;
const UInt32 sndFilenameFlag = 0x00020000;
private Sound(){}
}
// Enum for message beep types
enum BeepTypes{
Simple = -1,
Ok = 0x00000000,
IconHand = 0x00000010,
IconQuestion = 0x00000020,
IconExclamation = 0x00000030,
IconAsterisk = 0x00000040
}
}