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 ?


Author Comment

ID: 7117632
I mean you can see you change in windows volume control in control pannel
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.


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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now