[vb.net] Windows 시스템 오디오 볼륨 가져오기 및 설정 > vb.net

본문 바로가기

vb.net

[기타] [vb.net] Windows 시스템 오디오 볼륨 가져오기 및 설정

회원사진
하나를하더라도최선을
2024-06-04 16:22 186 0

본문



d3352cacf9e284a757626685bdad2d54_1717485565_6703.png 


 

소개

출처 입력

현재 시스템 출력 볼륨을 검색하거나 설정하는 메서드를 제공하는 .NET 클래스가 없습니다.

이 문서에서는 Windows 멀티미디어 장치 API에 대한 인터페이스를 사용하여 솔루션을 제공합니다.

 

배경

웹 페이지에서 이 기사에 대한 아이디어를 얻었습니다.

VB .NET 또는 C# 소스 코드에 삽입하여 즉시 사용할 수 있도록 조금 다듬었습니다.


 

코드 사용

현재 시스템 오디오 볼륨 설정값을 정수(0~100)로 검색하려면 함수를 호출하세요 GetVolume().

ex)

Dim currentVolume As Integer = GetVolume()

 

현재 시스템 오디오 볼륨 설정을 새 값(0 - 100)으로 설정하려면 함수를 호출하십시오 SetVolume().

ex)

SetVolume(30)

 

다음은 Windows 멀티미디어 API를 호출하여 Windows 시스템 오디오 볼륨을 가져오거나 설정하는 VB.NET 및 C# 메서드의 소스 코드입니다.

Option Strict Off
Imports System
Imports System.Runtime.InteropServices

Module WindowsSystemAudio
    <ComImport>
    <Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), _
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Private Interface IMMDeviceEnumerator
#Disable Warning IDE1006 ' Naming Styles
        Sub lpVtbl()
#Enable Warning IDE1006 ' Naming Styles
        Function GetDefaultAudioEndpoint(ByVal dataFlow As Integer, _
        ByVal role As Integer, <Out> ByRef ppDevice As IMMDevice) As Integer
    End Interface

    Private NotInheritable Class MMDeviceEnumeratorFactory
        Public Shared Function CreateInstance() As IMMDeviceEnumerator
            Return CType(Activator.CreateInstance_
            (Type.GetTypeFromCLSID(New Guid_
            ("BCDE0395-E52F-467C-8E3D-C4579291692E"))), _
            IMMDeviceEnumerator) ' a MMDeviceEnumerator
        End Function
    End Class

    <Guid("D666063F-1587-4E43-81F1-B948E807363F"), _
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Private Interface IMMDevice
        Function Activate(
    <MarshalAs(UnmanagedType.LPStruct)> ByVal iid As Guid, _
     ByVal dwClsCtx As Integer, ByVal pActivationParams As IntPtr, <Out>
    <MarshalAs(UnmanagedType.IUnknown)> ByRef ppInterface As Object) As Integer
    End Interface

    <Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), _
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Public Interface IAudioEndpointVolume
        Function RegisterControlChangeNotify(ByVal pNotify As IntPtr) As Integer
        Function UnregisterControlChangeNotify(ByVal pNotify As IntPtr) As Integer
        Function GetChannelCount(ByRef pnChannelCount As UInteger) As Integer
        Function SetMasterVolumeLevel(ByVal fLevelDB As Single, _
        ByVal pguidEventContext As Guid) As Integer
        Function SetMasterVolumeLevelScalar_
        (ByVal fLevel As Single, ByVal pguidEventContext As Guid) As Integer
        Function GetMasterVolumeLevel(ByRef pfLevelDB As Single) As Integer
        Function GetMasterVolumeLevelScalar(ByRef pfLevel As Single) As Integer
    End Interface

    Friend Sub SetVolume(ByVal Level As Integer)
        Try
            Dim deviceEnumerator As IMMDeviceEnumerator = _
                                 MMDeviceEnumeratorFactory.CreateInstance()
            Dim speakers As IMMDevice = Nothing
            Dim res As Integer
            Const eRender = 0
            Const eMultimedia = 1
            deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, speakers)
            Dim Audio_EndPointVolume As IAudioEndpointVolume = Nothing
            speakers.Activate(GetType(IAudioEndpointVolume).GUID, 0, _
                              IntPtr.Zero, Audio_EndPointVolume)
            Dim ZeroGuid As New Guid()
            res = Audio_EndPointVolume.SetMasterVolumeLevelScalar_
                  (Level / 100.0F, ZeroGuid)
        Catch ex As Exception
        End Try
    End Sub

    Friend Function GetVolume() As Integer
        Try
            Dim currentLevel As Single = 0 ' Expressed as a decimal value
            Dim deviceEnumerator As IMMDeviceEnumerator = _
                                 MMDeviceEnumeratorFactory.CreateInstance()
            Dim speakers As IMMDevice = Nothing
            Dim res As Integer
            Const eRender = 0
            Const eMultimedia = 1
            deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, speakers)
            Dim Audio_EndPointVolume As IAudioEndpointVolume = Nothing
            speakers.Activate(GetType(IAudioEndpointVolume).GUID, _
                              0, IntPtr.Zero, Audio_EndPointVolume)
            res = Audio_EndPointVolume.GetMasterVolumeLevelScalar(currentLevel)
            Return CInt(100 * currentLevel)  ' Returned as an Integer 0 - 100
        Catch ex As Exception
            Return -1
        End Try
    End Function
End Module 
 

 

 

 

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색