Changing audio volume and balance

This page shows you how to change the audio volume and balance. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

The following sample code shows how to change audio volume and balance. This sample changes audio volume and balance of a MP3 file.


#include <stdio.h>

#include <dshow.h>

// change here
#define	FILENAME L"C:\\DXSDK\\Samples\\Media\\piano2.mp3"

int
main()
{
 IGraphBuilder *pGraphBuilder;
 IMediaControl *pMediaControl;
 IBasicAudio *pBasicAudio;

 CoInitialize(NULL);

 CoCreateInstance(CLSID_FilterGraph,
	NULL,
	CLSCTX_INPROC,
	IID_IGraphBuilder,
	(LPVOID *)&pGraphBuilder);

 pGraphBuilder->QueryInterface(IID_IMediaControl,
	(LPVOID *)&pMediaControl);

 pGraphBuilder->QueryInterface(IID_IBasicAudio,
	(LPVOID *)&pBasicAudio);

 pMediaControl->RenderFile(FILENAME);

 /*
 You can change balance using put_Balance().
 The value can be between -10000 to 10000.
 -10000 will turn off the right side.
 On the other hand, 10000 will turn off the left side.
 This sample sets "0", which means
 the same value for left and right.
 */
 pBasicAudio->put_Balance(0);

 /*
 You can change volume using put_Volume().
 The allowed values are -10000 to 0.
 The loudest value is "0".
 -10000 is silence.
 This value will be 100 times of dB.
 For example, -5000 will be -50dB.
 */
 pBasicAudio->put_Volume(-500);

 pMediaControl->Run();

 MessageBox(NULL,
	"Block Execution",
	"Block",
	MB_OK);

 pMediaControl->Release();
 pGraphBuilder->Release();
 pBasicAudio->Release();

 CoUninitialize();

 return 0;
}


If you want to obtain the current audio volume, you can use pBasicAudio->get_Volume(long *).

If you want to obtain the current audio balance, you can use pBasicAudio->get_Balance(long *).

  

Copyright (C) GeekPage.JP. All rights reserved.