473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Volume control for a audio/video file

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

Jul 13 '06 #1
0 3467

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dreamer | last post by:
I am making a program for a friend with a disability. I need to adjust his audio volume using the keyboard as he cannot use a mouse. Any suggestions as to how I can raise the volume using the up...
0
by: sengkok | last post by:
hi, i am using the directshow in C# to built a karaoke system,but i am facing a problem with the audio play in the video file (DAT format), some of the video file , i can control the music and...
0
by: nan | last post by:
Please give me some tips or some source code by using which able to make the avi file for audio and video data to play in sync. Some details are given below: I noticed that many times the video...
0
by: Zoran | last post by:
Hi everybody!!! How can I get video properties from video file (framedim, framerate, compression..) and audio properties from audio file (Bitrate, audio resolution, compression..) Any help...
3
by: Andreas Hecker | last post by:
Hello, when i play a sound file like mp3 or wav with MediaPlayer or anything like this, i would like to get the volume level of the sound card in real time. The data is then sent to the serial...
7
by: Rich Milburn [MVP] | last post by:
Ok I am not a programmer, I copied some code and very painfully got it working in VB6. I can adjust the volume with waveOutSetVolume on winmm.dll. But I could not seem to be able to figure out how...
2
by: hzgt9b | last post by:
Using VS2003, VB.NET, BACKGROUND I have a window forms based application that will be distributed and executed directly from CD media. The app contains a TreeView control and a WebBroswer...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
0
by: RickH | last post by:
Has anyone wrapped the Windows Media Player ActiveX control as a custom DataGridView column type (prefereably in VB)? That might want to share the source code on the group? So that video/audio...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.