2009-03-29 8 views
4

Comment lire le son du système "New Mail" en C#? C'est ce qu'on appelle le son "Notify".Comment lire le son du système "New Mail" dans .Net?

Dans Win32, ce serait quelque chose comme

sndPlaySound('Notify', (SND_ALIAS or SND_ASYNC)); 

Alors, comment faites-vous cela en .Net? Je sais que vous pouvez faire

System.Media.SystemSounds.Asterisk.Play(); 

Mais il y a un nombre très limité de cinq sons là-bas - pas compris quel que soit le l'utilisateur a défini comme le nouveau son mail.

Je peux savoir quel fichier .wav est en cours de lecture lorsque je reçois un nouveau message et que je le lit, mais cela ne se met pas à jour lorsque le schéma sonore de l'utilisateur est modifié.


Ce que je finalement fait:

Au lieu de jouer un son système, j'intégré un fichier wav dans l'application comme une ressource, et a joué avec System.Media.SoundPlayer

Répondre

5

Une option est juste PInvoke directement dans l'API sndSound. Voici la définition PInvoke pour cette méthode

public partial class NativeMethods { 

    /// Return Type: BOOL->int 
    ///pszSound: LPCWSTR->WCHAR* 
    ///fuSound: UINT->unsigned int 
    [System.Runtime.InteropServices.DllImportAttribute("winmm.dll", EntryPoint="sndPlaySoundW")] 
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] 
public static extern bool sndPlaySoundW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pszSound, uint fuSound) ; 

    /// SND_APPLICATION -> 0x0080 
    public const int SND_APPLICATION = 128; 

    /// SND_ALIAS_START -> 0 
    public const int SND_ALIAS_START = 0; 

    /// SND_RESOURCE -> 0x00040004L 
    public const int SND_RESOURCE = 262148; 

    /// SND_FILENAME -> 0x00020000L 
    public const int SND_FILENAME = 131072; 

    /// SND_ALIAS_ID -> 0x00110000L 
    public const int SND_ALIAS_ID = 1114112; 

    /// SND_NOWAIT -> 0x00002000L 
    public const int SND_NOWAIT = 8192; 

    /// SND_NOSTOP -> 0x0010 
    public const int SND_NOSTOP = 16; 

    /// SND_MEMORY -> 0x0004 
    public const int SND_MEMORY = 4; 

    /// SND_PURGE -> 0x0040 
    public const int SND_PURGE = 64; 

    /// SND_ASYNC -> 0x0001 
    public const int SND_ASYNC = 1; 

    /// SND_ALIAS -> 0x00010000L 
    public const int SND_ALIAS = 65536; 

    /// SND_SYNC -> 0x0000 
    public const int SND_SYNC = 0; 

    /// SND_LOOP -> 0x0008 
    public const int SND_LOOP = 8; 

    /// SND_NODEFAULT -> 0x0002 
    public const int SND_NODEFAULT = 2; 
} 
3

En fait, le nouveau son de messagerie est l'alias « MailBeep », et non l'alias « Notifier ».

Vous voulez donc appeler PlaySound (L "MailBeep", NULL, SND_SYSTEM | SND_NODEFAULT | SND_ALIAS);

Et P/Invoke est certainement le chemin à parcourir.

Don't forget to specify SND_NODEFAULT or your app will make dings even if the user disables the new mail sound in the control panel. SND_SYSTEM est nouveau pour Windows Vista et fait que le son soit joué comme un "son windows" - c'est à vous de décider si c'est l'expérience que vous voulez avoir.

+0

Quelle est la valeur de SND_SYSTEM? – Pedro77

Questions connexes