473,549 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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

Jul 13 '06 #1
0 3528

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

Similar topics

3
19099
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 arrow on the keyboard and decrease the volume using the down arrow? I am using VB 5. Many thanks in advance.
0
1954
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 vocal play by using the BALANCE in the directshow. but some of the video file will only play the music and without the vocal . i have no idea why this...
0
1384
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 recorded is 7 Frames per second and again it will change to 15 frames per second. While in this scenario the audio card's sampling frequency is...
0
1328
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 will be appreciated Thanx,
3
6720
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 port. How do i get this data. I tried to find some samples in msdn or internet but i couldn t find any useful code. There is the winmm.dll which...
7
7637
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 to adjust the master volume. I thought maybe if I upgraded this into VS2005 VB.Net then it might be a little easier. But now I am getting...
2
7876
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 (AxSHDocVw.AxWebBrowse) control. The TreeView is populated with nodes that when clicked play an audio clip that is stored on the CD and navigate the...
13
4855
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, suggestions or improvements? if( wmv file) document.write("<OBJECT id=Player classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6 height="354"...
0
1199
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 clips can be viewed within a cell of a data grid view. This control would be bound to a database column that contains the file path/url string of...
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7461
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.