2013-06-03 83 views
2

我的應用程序需要能夠更改聲音設備的系統音量級別。我在用NAudio使用C#。我嘗試在NAudio中使用CoreAudio Api,但這在Windows XP中不起作用,但是我的程序需要支持XP。請幫助我,我需要什麼來使我的程序支持XP以及最新的Windows。在Windows中更改SYSTEM音量級別

+1

在這裏的樣子http://stackoverflow.com/questions/13139181/how-to-programmatically-set-the-system-volume –

+2

NAudio還包括混音器的包裝器......您應該可以使用的API調整系統音量在XP –

+0

@MarkHeath你能幫助我嗎?我需要搜索什麼? – EXTRAM

回答

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#

+2

我在Windows 8.1上。無論我的音頻級別如何,我總是可以獲得相同的值(65535),即使我將主音量靜音也是如此。 – BrunoLM

+1

在Windows 7中,它只讀取/寫入每個應用程序卷 – kritzikratzi