Hello ,
I am looking for help in controlling the volume of a media file in a
vb.net application. The code that I currently have (attached below)
tries to control the master volume which finally affects all
applications opened, whereas I want to control the volume only for my
application alone. I have a trackbar control on my form to scroll
values and increase/decrease the volume. How would I control the volume
for the media file opened in my application?
Thanks,
T S
Friend Sub SetVolume(ByVal Level As Integer)
' Sets the volume to a specific percentage as passed through
Dim hmixer As Integer
Dim volCtrl As New MIXERCONTROL
Dim lngReturn As Integer
Dim lngVolSetting As Integer
' Obtain the hmixer struct
lngReturn = mixerOpen(hmixer, 0, 0, 0, 0)
' Error check
If lngReturn <0 Then Exit Sub
' Obtain the volumne control object
Call GetVolumeControl(hmixer,
MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl)
' Then determine the value of the volume
lngVolSetting = CType(volCtrl.lMaximum * (Level / 100),
Integer)
' Then set the volume
SetVolumeControl(hmixer, volCtrl, lngVolSetting)
End Sub
Friend Sub SetSound(ByVal boolMute As Boolean)
' This routine sets the volume setting of the current unit
depending on the value passed through
Dim hmixer As Integer
Dim volCtrl As New MIXERCONTROL
Dim lngReturn As Integer
Dim lngVolSetting As Integer
' Obtain the hmixer struct
lngReturn = mixerOpen(hmixer, 0, 0, 0, 0)
' Error check
If lngReturn <0 Then Exit Sub
' Obtain the volumne control object
Call GetVolumeControl(hmixer,
MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, MIXERCONTROL_CONTROLTYPE_MUTE,
volCtrl)
' Then determine the value of the volume
If boolMute Then
' Mute
lngVolSetting = 1
Else
' Turn the sound on
lngVolSetting = 0
End If
' Then set the volume
SetVolumeControl(hmixer, volCtrl, lngVolSetting)
End Sub
Friend Function GetVolumeControl(ByVal hmixer As Integer, ByVal
componentType As Integer, ByVal ctrlType As Integer, _
ByRef mxc As MIXERCONTROL) As Boolean
' Obtains an appropriate pointer and info for the volume
control
' [Note: original source taken from MSDN
http://support.microsoft.com/default...N-US;Q178456&]
' This function attempts to obtain a mixer control. Returns
True if successful.
Dim mxlc As New MIXERLINECONTROLS
Dim mxl As New MIXERLINE
Dim rc As Integer, pmem As IntPtr
mxl.cbStruct = Marshal.SizeOf(mxl)
mxl.dwComponentType = componentType
' Obtain a line corresponding to the component type
rc = mixerGetLineInfo(hmixer, mxl,
MIXER_GETLINEINFOF_COMPONENTTYPE)
If (MMSYSERR_NOERROR = rc) Then
mxlc.cbStruct = Marshal.SizeOf(mxlc)
mxlc.dwLineID = mxl.dwLineID
mxlc.dwControl = ctrlType
mxlc.cControls = 1
mxlc.cbmxctrl = Marshal.SizeOf(mxc)
' Allocate a buffer for the control
pmem = Marshal.AllocHGlobal(Marshal.SizeOf(mxc))
mxlc.pamxctrl = pmem
mxc.cbStruct = Marshal.SizeOf(mxc)
' Get the control
rc = mixerGetLineControls(hmixer, _
mxlc, _
MIXER_GETLINECONTROLSF_ONEBYTYPE)
If (MMSYSERR_NOERROR = rc) Then
mxc = CType(Marshal.PtrToStructure(mxlc.pamxctrl,
GetType(MIXERCONTROL)), MIXERCONTROL)
Marshal.FreeHGlobal(pmem)
Return True
End If
Marshal.FreeHGlobal(pmem)
Exit Function
End If
Return False
End Function
Friend Function SetVolumeControl(ByVal hmixer As Integer, _
ByVal mxc As MIXERCONTROL, _
ByVal volume As Integer) As Boolean
' Sets the volumne from the pointer of the object passed
through
' [Note: original source taken from MSDN
http://support.microsoft.com/default...N-US;Q178456&]
'This function sets the value for a volume control. Returns
True if successful
Dim mxcd As MIXERCONTROLDETAILS
Dim vol As MIXERCONTROLDETAILS_UNSIGNED
Dim rc As Integer
Dim hptr As IntPtr
mxcd.item = 0
mxcd.dwControlID = mxc.dwControlID
mxcd.cbStruct = Marshal.SizeOf(mxcd)
mxcd.cbDetails = Marshal.SizeOf(vol)
hptr = Marshal.AllocHGlobal(Marshal.SizeOf(vol))
' Allocate a buffer for the control value buffer
mxcd.paDetails = hptr
mxcd.cChannels = 1
vol.dwValue = volume
Marshal.StructureToPtr(vol, hptr, False)
' Set the control value
rc = mixerSetControlDetails(hmixer, _
mxcd, _
0)
Marshal.FreeHGlobal(hptr)
If (MMSYSERR_NOERROR = rc) Then
Return True
Else
Return False
End If
End Function