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(hmixe r, 0, 0, 0, 0)
' Error check
If lngReturn <0 Then Exit Sub
' Obtain the volumne control object
Call GetVolumeContro l(hmixer,
MIXERLINE_COMPO NENTTYPE_DST_SP EAKERS, _
MIXERCONTROL_CO NTROLTYPE_VOLUM E, volCtrl)
' Then determine the value of the volume
lngVolSetting = CType(volCtrl.l Maximum * (Level / 100),
Integer)
' Then set the volume
SetVolumeContro l(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(hmixe r, 0, 0, 0, 0)
' Error check
If lngReturn <0 Then Exit Sub
' Obtain the volumne control object
Call GetVolumeContro l(hmixer,
MIXERLINE_COMPO NENTTYPE_DST_SP EAKERS, MIXERCONTROL_CO NTROLTYPE_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
SetVolumeContro l(hmixer, volCtrl, lngVolSetting)
End Sub
Friend Function GetVolumeContro l(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 MIXERLINECONTRO LS
Dim mxl As New MIXERLINE
Dim rc As Integer, pmem As IntPtr
mxl.cbStruct = Marshal.SizeOf( mxl)
mxl.dwComponent Type = componentType
' Obtain a line corresponding to the component type
rc = mixerGetLineInf o(hmixer, mxl,
MIXER_GETLINEIN FOF_COMPONENTTY PE)
If (MMSYSERR_NOERR OR = 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.AllocHG lobal(Marshal.S izeOf(mxc))
mxlc.pamxctrl = pmem
mxc.cbStruct = Marshal.SizeOf( mxc)
' Get the control
rc = mixerGetLineCon trols(hmixer, _
mxlc, _
MIXER_GETLINECO NTROLSF_ONEBYTY PE)
If (MMSYSERR_NOERR OR = rc) Then
mxc = CType(Marshal.P trToStructure(m xlc.pamxctrl,
GetType(MIXERCO NTROL)), MIXERCONTROL)
Marshal.FreeHGl obal(pmem)
Return True
End If
Marshal.FreeHGl obal(pmem)
Exit Function
End If
Return False
End Function
Friend Function SetVolumeContro l(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 MIXERCONTROLDET AILS
Dim vol As MIXERCONTROLDET AILS_UNSIGNED
Dim rc As Integer
Dim hptr As IntPtr
mxcd.item = 0
mxcd.dwControlI D = mxc.dwControlID
mxcd.cbStruct = Marshal.SizeOf( mxcd)
mxcd.cbDetails = Marshal.SizeOf( vol)
hptr = Marshal.AllocHG lobal(Marshal.S izeOf(vol))
' Allocate a buffer for the control value buffer
mxcd.paDetails = hptr
mxcd.cChannels = 1
vol.dwValue = volume
Marshal.Structu reToPtr(vol, hptr, False)
' Set the control value
rc = mixerSetControl Details(hmixer, _
mxcd, _
0)
Marshal.FreeHGl obal(hptr)
If (MMSYSERR_NOERR OR = rc) Then
Return True
Else
Return False
End If
End Function