2015-11-19 51 views
0

我正在使用NAudio作为我正在设计的屏幕录制软件,我需要知道是否有可能不仅控制特定应用程序的音量,而且还显示应用程序声音的VU表。控制应用程序的音量和VU表

我已经遍布各地搜索,似乎我只能得到我的电脑上目前设备的VU表,并设置这些设备的音量。

尽管我使用的是NAudio,但我仍对其他解决方案开放。

回答

1

在这个问题后,我更详细地问了问题。我从那以后找到了答案,所以我将在这里为那些偶然发现的人留下答案。试图使用NAudio & CSCore让我很熟悉,所以请询问是否需要进一步的帮助。

的代码块使用CSCore,并在这里找到了答案的修改和注释版本:Getting individual windows application current volume output level as visualized in audio Mixer

class PeakClass 
{ 
    static int CurrentProcessID = 0000; 

    private static void Main(string[] args) 
    { 
     //Basically gets your default audio device and session attached to it 
     using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render)) 
     { 
      using (var sessionEnumerator = sessionManager.GetSessionEnumerator()) 
      { 
       //This will go through a list of all processes uses the device 
       //the code got two line above. 
       foreach (var session in sessionEnumerator) 
       { 
        //This block of code will get the peak value(value needed for VU Meter) 
        //For whatever process you need it for (I believe you can also check by name 
        //but I found that less reliable) 
        using (var session2 = session.QueryInterface<AudioSessionControl2>()) 
        { 
         if(session2.ProcessID == CurrentProcessID) 
         { 
          using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>()) 
          { 
           Console.WriteLine(audioMeterInformation.GetPeakValue()); 
          } 
         } 
        } 

        //Uncomment this block of code if you need the peak values 
        //of all the processes 
        // 
        //using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>()) 
        //{ 
        // Console.WriteLine(audioMeterInformation.GetPeakValue()); 
        //} 
       } 
      } 
     } 
    } 

    private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow) 
    { 
     using (var enumerator = new MMDeviceEnumerator()) 
     { 
      using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia)) 
      { 
       Console.WriteLine("DefaultDevice: " + device.FriendlyName); 
       var sessionManager = AudioSessionManager2.FromMMDevice(device); 
       return sessionManager; 
      } 
     } 
    } 
} 

下面的代码块将允许您使用更改设备的音量n音讯

MMDevice VUDevice; 

public void SetVolume(float vol) 
    { 
     if(vol > 0) 
     { 
      VUDevice.AudioEndpointVolume.Mute = false; 
      VUDevice.AudioEndpointVolume.MasterVolumeLevelScalar = vol; 
     } 
     else 
     { 
      VUDevice.AudioEndpointVolume.Mute = true; 
     } 
     Console.WriteLine(vol); 
    } 

我有两个不同的库的代码只是为了回答我直接发布的问题,即如何设置音量并获得VU仪表值(峰值)。 CSCore和NAudio非常相似,所以这里的大部分代码都是可以互换的。