การควบคุมระดับเสียงและ VU Meter ของแอปพลิเคชัน

ฉันกำลังใช้ NAudio สำหรับซอฟต์แวร์บันทึกหน้าจอที่ฉันกำลังออกแบบ และฉันจำเป็นต้องทราบว่าเป็นไปได้หรือไม่ที่ไม่เพียงแต่จะควบคุมระดับเสียงของแอปพลิเคชันเฉพาะเท่านั้น แต่ยังแสดง VU Meter สำหรับเสียงของแอปพลิเคชันด้วย

ฉันใช้ Google ไปทั่วและดูเหมือนว่าฉันจะได้เฉพาะ VU Meter สำหรับอุปกรณ์ที่อยู่ในคอมพิวเตอร์ของฉันในปัจจุบันและตั้งค่าระดับเสียงสำหรับอุปกรณ์เหล่านั้น

แม้ว่าฉันจะใช้ NAudio แต่ฉันก็ยังเปิดรับวิธีแก้ปัญหาอื่นๆ


person Asix Jin    schedule 19.11.2015    source แหล่งที่มา


คำตอบ (1)


ฉันถามคำถามโดยละเอียดเพิ่มเติมหลังจากคำถามนี้ ฉันได้พบคำตอบแล้ว ดังนั้นฉันจะทิ้งคำตอบไว้ที่นี่สำหรับผู้ที่สะดุดกับมัน การพยายามใช้ NAudio และ CSCore ทำให้ฉันคุ้นเคยดี ดังนั้นโปรดถามว่าคุณต้องการความช่วยเหลือเพิ่มเติมหรือไม่

บล็อกโค้ดนี้ใช้ CSCore และเป็นคำตอบในเวอร์ชันที่ได้รับการแก้ไขและแสดงความคิดเห็นได้ที่นี่:การรับระดับเอาต์พุตระดับเสียงปัจจุบันของแอปพลิเคชัน windows แต่ละเครื่องตามที่มองเห็นในเครื่องผสมเสียง

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;
            }
        }
    }
} 

บล็อกโค้ดต่อไปนี้จะช่วยให้คุณสามารถเปลี่ยนระดับเสียงของอุปกรณ์โดยใช้ NAudio

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 Meter (ค่าสูงสุด) CSCore และ NAudio มีความคล้ายคลึงกันมาก ดังนั้นโค้ดส่วนใหญ่ที่นี่จึงใช้แทนกันได้

person Asix Jin    schedule 16.12.2015