2010-03-28 32 views
20

Ho bisogno di ottenere il volume corrente dell'uscita sulla scheda audio.Ottieni il volume audio principale in C#

Qualche idea come?

+0

Vuoi il volume corrente, o il volume corrente massima? Se si desidera il volume massimo attuale, questo è apparentemente "diverso" per Vista + rispetto a XP, vedere http://stackoverflow.com/q/4640106/32453 – rogerdpack

+0

Un'altra soluzione è questo articolo (anche) da Progetto codice: [VolumeMeter (Managed DirectX)] (http://www.codeproject.com/KB/directx/volumemeter.aspx) – loraderon

risposta

12

È possibile ottenere a questi valori utilizzando IAudioMeterInformation nelle API CoreAudio in Vista e Win 7.

involucri Managed sono disponibili in NAudio (arrivare alla AudioMeterInformation dalla MMDevice).

2

Cerca in informazioni MSDN per:

  • IMMDeviceCollection, IMMDevice e IAudioEndpointVolume (solo Windows Vista, Windows 7).
  • mixerGetNumDevs, mixerGetLineControls, ...

Si tratta di informazioni "comune". È possibile che C# abbia modi più convenienti (non lo so).

+0

Chiede l'effettiva misurazione del volume di uscita audio in tempo reale sul canale Master, non una maniglia per impostare il selettore del mixer. Vedi la parte che ha selezionato sullo screenshot? Per quanto posso dire, quelle API Win non offrono accesso a tali informazioni "in tempo reale". –

+1

Mike Atlas, hai ragione. In questo caso dovrebbe essere usato IAudioMeterInformation (come indicato da Mark Heath). – VitalyVal

0

Partenza questo codice da Codice Progetto: LED Style Volume Meter Using DirectX

Questo articolo serve come un manuale di utilizzo per un UserControl che ho creato chiamato AnalogSignalMeter. Questo controllo utilizza Direct3D per dipingere il controllo e DirectSound per campionare il segnale audio .

Ha un oggetto AnalogSignalMeter, che genera un evento che riporterà il livello attuale dell'altoparlante sinistro e destro.

0

Non credo ci sia un modo semplice per ottenere il picco corrente sotto XP. MIXERCONTROL_CONTROLTYPE_PEAKMETER è presente ma credo che sia in gran parte non supportato (è sul mio computer attuale). Immagino che avrai creato il tuo metodo di analisi dell'uscita audio corrente, dai un'occhiata alla sezione DSP here.

Si può solo decidere in fase di esecuzione quale metodo si desidera utilizzare, XP e Vista/7 hanno metodi molto diversi per gestire l'audio. Alcune informazioni utili sull'argomento che ho scritto in precedenza possono essere here.

La documentazione MSDN e il blog di Larry Osterman (è anche membro di SO) sono probabilmente le 2 fonti più utili per l'infrastruttura audio Windows corrente a mio parere.

1

Forse il winmm.dll può aiutare:

Da EDDYKT (VB):

Private Const HIGHEST_VOLUME_SETTING = 100 '% 
Private Const AUX_MAPPER = -1& 
Private Const MAXPNAMELEN = 32 
Private Const AUXCAPS_CDAUDIO = 1 ' audio from internal CD-ROM drive 
Private Const AUXCAPS_AUXIN = 2 ' audio from auxiliary input jacks 
Private Const AUXCAPS_VOLUME = &H1   ' supports volume control 
Private Const AUXCAPS_LRVOLUME = &H2   ' separate left-right volume control 
Private Const MMSYSERR_NOERROR = 0 
Private Const MMSYSERR_BASE = 0 
Private Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2) 
Private Type AUXCAPS 
     wMid As Integer 
     wPid As Integer 
     vDriverVersion As Long 
     szPname As String * MAXPNAMELEN 
     wTechnology As Integer 
     dwSupport As Long 
End Type 
Private Type VolumeSetting 
    LeftVol As Integer 
    RightVol As Integer 
End Type 
Private Declare Function auxGetNumDevs Lib "winmm.dll"() As Long 
Private Declare Function auxGetDevCaps Lib "winmm.dll" Alias "auxGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As AUXCAPS, ByVal uSize As Long) As Long 
Private Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long 
Private Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByRef lpdwVolume As VolumeSetting) As Long 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) 
Private Function nSigned(ByVal lUnsignedInt As Long) As Integer 
    Dim nReturnVal As Integer       ' Return value from Function 
    If lUnsignedInt > 65535 Or lUnsignedInt < 0 Then 
     MsgBox "Error in conversion from Unsigned to nSigned Integer" 
     nSignedInt = 0 
     Exit Function 
    End If 
    If lUnsignedInt > 32767 Then 
     nReturnVal = lUnsignedInt - 65536 
    Else 
     nReturnVal = lUnsignedInt 
    End If 
    nSigned = nReturnVal 
End Function 
Private Function lUnsigned(ByVal nSignedInt As Integer) As Long 
    Dim lReturnVal As Long       ' Return value from Function 
    If nSignedInt < 0 Then 
     lReturnVal = nSignedInt + 65536 
    Else 
     lReturnVal = nSignedInt 
    End If 
    If lReturnVal > 65535 Or lReturnVal < 0 Then 
     MsgBox "Error in conversion from nSigned to Unsigned Integer" 
     lReturnVal = 0 
    End If 
    lUnsigned = lReturnVal 
End Function 
Private Function lSetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long 
    Dim Volume As VolumeSetting, lBothVolumes As Long 
    Volume.LeftVol = nSigned(lLeftVol * 65535/HIGHEST_VOLUME_SETTING) 
    Volume.RightVol = nSigned(lRightVol * 65535/HIGHEST_VOLUME_SETTING) 
    'copy our Volume-variable to a long 
    CopyMemory lBothVolumes, Volume.LeftVol, Len(Volume) 
    'call the SetVolume-function 
    lSetVolume = auxSetVolume(lDeviceID, lBothVolumes) 
End Function 
Private Sub Form_Load() 
    'KPD-Team 2000 
    'URL: http://www.allapi.net/ 
    'E-Mail: [email protected] 
    Dim Volume As VolumeSetting, Cnt As Long, AC As AUXCAPS 
    'set the output to a persistent graphic 
    Me.AutoRedraw = True 
    'loop through all the devices 
    For Cnt = 0 To auxGetNumDevs - 1 'auxGetNumDevs is zero-based 
     'get the volume 
     auxGetVolume Cnt, Volume 
     'get the device capabilities 
     auxGetDevCaps Cnt, AC, Len(AC) 
     'print the name on the form 
     Me.Print "Device #" + Str$(Cnt + 1) + ": " + Left(AC.szPname, InStr(AC.szPname, vbNullChar) - 1) 
     'print the left- and right volume on the form 
     Me.Print "Left volume:" + Str$(HIGHEST_VOLUME_SETTING * lUnsigned(Volume.LeftVol)/65535) 
     Me.Print "Right volume:" + Str$(HIGHEST_VOLUME_SETTING * lUnsigned(Volume.RightVol)/65535) 
     'set the left- and right-volume to 50% 
     lSetVolume 50, 50, Cnt 
     Me.Print "Both volumes now set to 50%" 
     'empty line 
     Me.Print 
    Next 
End Sub 

O forse questo: http://blackbeltvb.com/index.htm?free/mcisamp.htm

+0

Sembra che stia impostando il volume, penso che stiano chiedendo l'uscita audio corrente o quanto sia forte il suono emesso dagli altoparlanti. – GalacticJello

+0

Oh ho capito che chiedeva il valore del volume audio: le giunture auxGetVolume per fornire queste informazioni. Quindi forse auxGetDevCaps può essere di aiuto per il dispositivo di output corrente? – JoeBilly

2

Ho risolto questo quando stavo lavorando su un (ancora ancora da rilasciare ...) applicazione che avvia una sorta di "musica dell'ascensore" quando non è presente nessun altro suono.

seguito la brillante punte date da Mark Heath, ho ottenuto quello che volevo:

using NAudio.CoreAudioApi; 
MMDeviceEnumerator devEnum = new MMDeviceEnumerator(); 
MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); 
string currVolume = "MasterPeakVolume : " + defaultDevice.AudioMeterInformation.MasterPeakValue.ToString(); 
+0

Questo non funziona su XP. – svick

+1

Ha funzionato! Grazie! (è necessario aggiungere NAudio da Nuget) –

3
static int PlayerVolume() 
    { 
     RecordPlayer rp = new RecordPlayer(); 
     rp.PlayerID = -1; 
     int playerVolume = rp.PlayerVolume; 
     return playerVolume; 
    } 

da modificato Microphone Volume in c# articolo

Problemi correlati