PlaySound (coredll)
Last changed: -65.10.102.204

.
Summary
TODO - a short description

C# Signature:

[DllImport("coredll.dll")]

public static extern int PlaySound(

    string szSound,
    IntPtr hModule,
    int flags);

public enum PlaySoundFlags : int {

    SND_SYNC = 0x0,     // play synchronously (default)
    SND_ASYNC = 0x1,    // play asynchronously
    SND_NODEFAULT = 0x2,    // silence (!default) if sound not found
    SND_MEMORY = 0x4,       // pszSound points to a memory file
    SND_LOOP = 0x8,     // loop the sound until next sndPlaySound
    SND_NOSTOP = 0x10,      // don't stop any currently playing sound
    SND_NOWAIT = 0x2000,    // don't wait if the driver is busy
    SND_ALIAS = 0x10000,    // name is a registry alias
    SND_ALIAS_ID = 0x110000,// alias is a predefined ID
    SND_FILENAME = 0x20000, // name is file name
    SND_RESOURCE = 0x40004, // name is resource name or atom

};

VB Signature:

    <DllImport("coredll.dll")> _
Public Shared Function PlaySound( _
  ByVal szSound As String, _
  ByVal hModule As IntPtr, _
  ByVal flags As Integer) As Integer
    End Function

User-Defined Types:

    Private Enum PlaySoundFlags
        SND_SYNC = &H0          ' play synchronously (default)
        SND_ASYNC = &H1          ' play asynchronously
        SND_NODEFAULT = &H2          ' silence (!default) if sound not found
        SND_MEMORY = &H4          ' pszSound points to a memory file
        SND_LOOP = &H8          ' loop the sound until next sndPlaySound
        SND_NOSTOP = &H10          ' don't stop any currently playing sound
        SND_NOWAIT = &H2000          ' don't wait if the driver is busy
        SND_ALIAS = &H10000          ' name is a registry alias
        SND_ALIAS_ID = &H110000          ' alias is a predefined ID
        SND_FILENAME = &H20000          ' name is file name
        SND_RESOURCE = &H40004          ' name is resource name or atom
    End Enum

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

C#

using System;

using System.ComponentModel;

using System.Runtime.InteropServices;

using SerialNET;

namespace Sound

{

    public enum PlaySoundFlags : int {
    SND_SYNC = 0x0,     // play synchronously (default)
    SND_ASYNC = 0x1,    // play asynchronously
    SND_NODEFAULT = 0x2,    // silence (!default) if sound not found
    SND_MEMORY = 0x4,       // pszSound points to a memory file
    SND_LOOP = 0x8,     // loop the sound until next sndPlaySound
    SND_NOSTOP = 0x10,      // don't stop any currently playing sound
    SND_NOWAIT = 0x2000,    // don't wait if the driver is busy
    SND_ALIAS = 0x10000,    // name is a registry alias
    SND_ALIAS_ID = 0x110000,// alias is a predefined ID
    SND_FILENAME = 0x20000, // name is file name
    SND_RESOURCE = 0x40004, // name is resource name or atom
    };

    public class Sound
    {
    [DllImport("coredll.dll")]
    public static extern int PlaySound(
        string szSound,
        IntPtr hModule,
        int flags);

    public static void Beep() {
        Play(@"\Windows\Voicbeep");
    }

    public static void Play(string fileName) {
        try {
        PlaySound(fileName, IntPtr.Zero, (int)(PlaySoundFlags.SND_FILENAME | PlaySoundFlags.SND_SYNC));
        } catch (Exception ex) {
        MessageBox.Show("Can't play sound file. " + ex.ToString());
        }
    }
    }

}

VB

Imports System

Imports System.Diagnostics

Imports System.IO

Imports System.Runtime.InteropServices

Public Class Sounds

    <Flags()> _
    Private Enum PlaySoundFlags
        SND_SYNC = &H0          ' play synchronously (default)
        SND_ASYNC = &H1          ' play asynchronously
        SND_NODEFAULT = &H2          ' silence (!default) if sound not found
        SND_MEMORY = &H4          ' pszSound points to a memory file
        SND_LOOP = &H8          ' loop the sound until next sndPlaySound
        SND_NOSTOP = &H10          ' don't stop any currently playing sound
        SND_NOWAIT = &H2000          ' don't wait if the driver is busy
        SND_ALIAS = &H10000          ' name is a registry alias
        SND_ALIAS_ID = &H110000          ' alias is a predefined ID
        SND_FILENAME = &H20000          ' name is file name
        SND_RESOURCE = &H40004          ' name is resource name or atom
    End Enum

    <DllImport("coredll.dll")> _
Public Shared Function PlaySound( _
  ByVal szSound As String, _
  ByVal hModule As IntPtr, _
  ByVal flags As Integer) As Integer
    End Function

    Public Shared Sub Play(ByVal fileName As String)
        Try
            PlaySound(fileName, IntPtr.Zero, PlaySoundFlags.SND_FILENAME Or _
              PlaySoundFlags.SND_SYNC)
        Catch ex As Exception
            MessageBox.Show("Can't play sound file. " & ex.ToString)
        End Try
    End Sub

End Class

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
PlaySound on MSDN