【问题标题】:Change SYSTEM volume level in windows在 Windows 中更改 SYSTEM 音量级别
【发布时间】:2013-06-03 10:12:26
【问题描述】:

我的应用程序需要能够更改声音设备的系统音量。我将 C# 与 NAudio 一起使用。我尝试在 NAudio 中使用 CoreAudio Api,但这在 Windows XP 中不起作用,但是我的程序需要支持 XP。请帮助我,我需要使用什么来让我的程序支持 XP 以及最新的 Windows。

【问题讨论】:

  • NAudio 还包括混音器的包装器...您应该能够使用这些 API 在 XP 中调整系统音量
  • @MarkHeath 你能帮帮我吗?我需要搜索什么?
  • 查看 Mixer.Mixers,然后查看每个混音器的 Destinations。希望你能找到你需要调整的音量控制
  • 谢谢@MarkHeath,这很有帮助! :)

标签: c# audio volume naudio


【解决方案1】:

这是使用 P/Invoke 调用的最简单且众所周知的方法:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace VolumeControl
    {
       public partial class Form1 : Form
       {
          [DllImport("winmm.dll")]
          public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

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

      public Form1()
      {
         InitializeComponent();
         // By the default set the volume to 0
         uint CurrVol = 0;
         // At this point, CurrVol gets assigned the volume
         waveOutGetVolume(IntPtr.Zero, out CurrVol);
         // Calculate the volume
         ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
         // Get the volume on a scale of 1 to 10 (to fit the trackbar)
         trackWave.Value = CalcVol / (ushort.MaxValue / 10);
      }

      private void trackWave_Scroll(object sender, EventArgs e)
      {
         // Calculate the volume that's being set
         int NewVolume = ((ushort.MaxValue / 10) * trackWave.Value);
         // Set the same volume for both the left and the right channels
         uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
         // Set the volume
         waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
      }
   }
}

来源:http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html

此外,如果您希望将其与 CoreAudioAPI 结合使用 - 请阅读:Change master audio volume from XP to Windows 8 in C#

【讨论】:

  • 我使用的是 Windows 8.1。无论我的音频电平如何,我总是得到相同的值 (65535),即使我将主音量静音。
  • 在 Windows 7 中,这仅读取/写入每个应用程序的卷
猜你喜欢
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多