MCI or WAFEFORM volume change effect global windows volume settings

Posted on 2002-06-27
Last Modified: 2007-12-19
Hi All,
I got a big problem:
When I play back wave file and change volume,the change will affect windows global volume settings (I mean you can see you change in windows volume control in control pannel).
I have tried:
1.MCI. mciSendCommand("setaudio thename valume to ....  When opened as avivideo
2.WaveForm. waveoutSetVolume()
Both of them working the same way.
Does someone have any idea how can I change volume for currently open waveaudio file only?

I prefer to open the wave file as waveaudio using mci.
Question by:soferstam
  • 5
  • 4

Expert Comment

ID: 7117492
Declare this API function in a standard module:

Public Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long

Public dvcid As Long ' these two variables will contain API function values
Public VolMedia As Long

Add a form to your project with a Command button (Command1) and a MCI control (MMC1), set the PlayEnabled,StopEnabled,PrevEnabled,NextEnabled properties to true and paste this code:

Dim FileSound as string

Private Sub Form_Load()

FileSound = "Mysound.wav" 'this is a sample wav file'

MMC1.Notify = False  'initialize MCI control
MMC1.Wait = True
MMC1.Shareable = False
MMC1.FileName = App.Path & "\" & FileSound 'open MCI control'
MMC1.Command = "Open"

dvcid = MMC1.DeviceID

InputString = InputBox("Enter a value from 0 to 65535", "MCI Volume setting")

VolMedia = Val(InputString)
End Sub

Private Sub MMC1_PlayClick(Cancel As Integer) 'Start playing wavesound
Call waveOutSetVolume(ByVal dvcid, VolMedia)  'adjust volume without modifiyng win control panel'

End Sub

Private Sub Command1_Click() 'This will reset the volume scale as before  starting the program'
VolMedia = &HFFFF
Call waveOutSetVolume(ByVal dvcid, VolMedia)

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MMC1.Command = "Close" 'close your MCI control when you close the form'

End Sub

Notice that playing your wavesound file doesn't alter the volume control setting. However you must click command1 after playing sound, otherwise you will get an altered volume scale when you check your windows volume control.

It must be stressed that Volume settings are interpreted logarithmically. This means the perceived increase in volume is the same when increasing the volume level from 0x5000 to 0x6000 as it is from 0x4000 to 0x5000.

Hope this helps

Author Comment

ID: 7117616
It's good to know,but I have a reason to use API calls only and not the MMC control.
I'm doing more or less the same:
Open file using mciSendCommand("open...
Play file using mciSendCommand("play...
Get Device ID using mciGetDeviceID("my alias name...
Change volume using waveOutSetVolume(ByVal dvcid, VolMedia)
The device id I'm passing correctly.But the volume change going to be for master volume....

Expert Comment

ID: 7117628
>>But the volume change going to be for master volume...

What do you mean exactly ? Are you playing another file with a different device, that is affected by the waveOutSetVolume function ? Or do you get a change in the volume control level in the win control panel ?

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 7117632
I mean you can see you change in windows volume control in control pannel

Expert Comment

ID: 7117652
When I tested my code the SETTINGS of the volume control were unchanged ( the level of the bar when you double click on the icon in the tray bar). However I found that the max volume output of my PC was changed  and it was equal to the volume set by the waveOutSetVolume function.
Therefore, if you use the code in the command1 click event
before closing the MCI control, you can restore the normal max volume output leaving unchanged the win volume control settings.

Private Sub Command1_Click()
VolMedia = &HFFFF
Call waveOutSetVolume(ByVal dvcid, VolMedia)
End Sub


Author Comment

ID: 7117673
I will test it on Monday.
I know that if you open 2 instanses of Windows Media Player and play two different files at the same time and change volume on one of them it is not going to effect another.This is what I'm trying to simulate.

Accepted Solution

dmarco earned 300 total points
ID: 7120626
Win Media Player uses DirectX to output wave sound.
If you want to change independently the volume settings of multiple running instances of your application , I believe you can't do it using MCI control. Probably you have to use DirectX (DirectSound) in your code: this way you can have direct access to the hardware.
If you are interested to add DirectSound to your app., let me know and I'll try to find some code for you.

However a workaround can be tried using MCI control. Add the following code to the previous I have posted:
   'Open a second MCI control (MMC2) in the form_load event'

FileSound2 = "Mysound2.wav" 'this is a file different from that played by MMC1
MMC2.Notify = False
MMC2.Wait = True
MMC2.Shareable = False

MMC2.FileName = App.Path & "\" & FileSound2
MMC2.Command = "Open"

   'Enter another volume setting for MMC2 control'

InputString = InputBox("Enter a value from 0 to 65535", "MCI2 Volume setting")
VolMedia2 = Val(InputString)

  'Add the following event driven routines'

Private Sub MMC1_LostFocus()
MMC1.Command = "Pause"
End Sub

Private Sub MMC2_LostFocus()
MMC2.Command = "Pause"
End Sub

Private Sub MMC2_PlayClick(Cancel As Integer)
Call waveOutSetVolume(dvcid, VolMedia2)
End Sub

Using this code you can play 2 (or more) wav files at different volume settings and when your application is closed, you can get the general Windows volume setting unchanged. Of course you can play the MCI controls one AFTER the other and NOT all at the same time. If you need this last option, I believe you need DirectSound help....
Don't forget the restore routine (Command1_Click button code), in order to reset the max volume output of Windows after closing your program.

Good luck!   Marco

Author Comment

ID: 7122515
I'm not sure if I will go to DirectX.
If yes I will post another question during this week.

Expert Comment

ID: 7123212
Glad I could help you.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question