Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅适用于我的应用程序的静音SoundPlayer_C#_Audio - Fatal编程技术网

C# 仅适用于我的应用程序的静音SoundPlayer

C# 仅适用于我的应用程序的静音SoundPlayer,c#,audio,C#,Audio,我希望仅为我的WPF应用程序静音,并将整个混音器保留为用户设置的混音器 我可以使用以下代码静音/取消静音系统范围的声音 但我注意到,当我的应用程序运行并播放声音时,我的应用程序会显示在windows mixer中,我可以通过mixer的UI仅禁用/取消禁用我的应用程序,因此我的应用程序似乎可以通过编程实现 private const int APPCOMMAND_VOLUME_MUTE = 0x80000; private const int WM_APPCOMMAND = 0x319; [D

我希望仅为我的WPF应用程序静音,并将整个混音器保留为用户设置的混音器

我可以使用以下代码静音/取消静音系统范围的声音

但我注意到,当我的应用程序运行并播放声音时,我的应用程序会显示在windows mixer中,我可以通过mixer的UI仅禁用/取消禁用我的应用程序,因此我的应用程序似乎可以通过编程实现

private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;

[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr) APPCOMMAND_VOLUME_MUTE);

这适用于Vista/7/8,其中有每个应用程序的音量控制

DllImport("winmm.dll")]
private static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

[DllImport("winmm.dll")]
private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);

/// <summary>
/// Returns volume from 0 to 10
/// </summary>
/// <returns>Volume from 0 to 10</returns>
public static int GetVolume()
{
  uint CurrVol = 0;
  waveOutGetVolume(IntPtr.Zero, out CurrVol);
  ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
  int volume = CalcVol / (ushort.MaxValue / 10);
  return volume;
}

/// <summary>
/// Sets volume from 0 to 10
/// </summary>
/// <param name="volume">Volume from 0 to 10</param>
public static void SetVolume(int volume)
{
  int NewVolume = ((ushort.MaxValue / 10) * volume);
  uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
  waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
}
DllImport(“winmm.dll”)]
专用静态外部int waveOutGetVolume(IntPtr hwo、out uint dwVolume);
[DllImport(“winmm.dll”)]
专用静态外部int waveOutSetVolume(IntPtr hwo、uint dwVolume);
/// 
///返回从0到10的卷
/// 
///从0到10的音量
公共静态int GetVolume()
{
uint CurrVol=0;
waveOutGetVolume(IntPtr.Zero,out CurrVol);
ushort CalcVol=(ushort)(CurrVol&0x0000ffff);
int volume=CalcVol/(ushort.MaxValue/10);
返回量;
}
/// 
///将音量从0设置为10
/// 
///从0到10的音量
公共静态void SetVolume(int volume)
{
int NewVolume=((ushort.MaxValue/10)*卷);

uint NewVolumeAllChannels=((uint)NewVolume&0x0000ffff)|((uint)NewVolume如果您包装调用以播放声音,您可以让这些函数检查一些资源以指示用户是否选择静音,例如:

public void PlaySoundXYZ() 
{
   if(!MuteSource.IsMuted()) 
   {
        // play sound.
   }
}

我已编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。这可能是重复的答案。这是为应用程序设置音量的正确方法,但不是将其静音。在windows混音器中静音将记住“取消静音”的上一个值,将其设置为0将不会。