MCI or WAFEFORM volume change effect global windows volume settings

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
soferstamAuthor Commented:
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....
>>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 ?

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

soferstamAuthor Commented:
I mean you can see you change in windows volume control in control pannel
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

soferstamAuthor Commented:
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
soferstamAuthor Commented:
I'm not sure if I will go to DirectX.
If yes I will post another question during this week.
Glad I could help you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.