2015-11-19 50 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非常相似,所以這裏的大部分代碼都是可以互換的。