Solved

MCI or WAFEFORM volume change effect global windows volume settings

Posted on 2002-06-27
9
642 Views
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.
0
Comment
Question by:soferstam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 2

Expert Comment

by:dmarco
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
0
 
LVL 1

Author Comment

by:soferstam
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....
0
 
LVL 2

Expert Comment

by:dmarco
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 ?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:soferstam
ID: 7117632
I mean you can see you change in windows volume control in control pannel
0
 
LVL 2

Expert Comment

by:dmarco
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

0
 
LVL 1

Author Comment

by:soferstam
ID: 7117673
Thanks.
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.
0
 
LVL 2

Accepted Solution

by:
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
0
 
LVL 1

Author Comment

by:soferstam
ID: 7122515
Thanks.
I'm not sure if I will go to DirectX.
If yes I will post another question during this week.
0
 
LVL 2

Expert Comment

by:dmarco
ID: 7123212
Glad I could help you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

717 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