An implementation class using Windows API to control playback volume and recording microphone volume (C + + code)

Source: Internet
Author: User
1, header file (Mixer.h)

Mixer.h:interface for the Cmixer class. #if!defined (afx_mixer_h__7369baa5_012e _467E_AD1E_A383E4474C58__INCLUDED_) #define AFX_MIXER_H__7369BAA5_012E_467E_AD1E_A383E4474C58__INCLUDED_ #define Win32_lean_and_mean//Exclude rarely-used stuff from Windows headers #include "windows.h" #include <mmsystem.h> #if _msc_ver > 1000 #pragma once #endif//_msc_ver > 1000 class Cmixer {public:cmixer (); Cmixer (const int volrange); Virtual ~cmixer (); Enum mixerdeice{speakers=0, Waveout, synthesizer, microphone}; BOOL Getmute (Mixerdeice Dev); Check if the device is mute bool Setmute (Mixerdeice Dev,bool vol.); Set Device mute bool SetVolume (Mixerdeice Dev,long vol.); Set the volume of the device unsigned getvolume (Mixerdeice dev); Gets the volume of the device dev=0 the main volume, 1WAVE, 2MIDI, 3 line in Private:bool Getvolumecontrol (Hmixer hmixer, long Componenttype,long Ctrltype, mixercontrol* MXC); BOOL Setvolumevalue (hmixer hmixer, Mixercontrol *mxc, long volume); BOOL SetmutevaluE (hmixer hmixer, Mixercontrol *MXC, bool mute); Unsigned getvolumevalue (hmixer hmixer, Mixercontrol *MXC); Long Getmutevalue (hmixer hmixer, Mixercontrol *MXC); Long M_volrange; }; #endif//!defined (AFX_MIXER_H__7369BAA5_012E_467E_AD1E_A383E4474C58__INCLUDED_) 2, CPP file (Mixer.cpp)

Mixer.cpp:implementation of the Cmixer class. #include "stdwx.h" #include "Mixer.h"/////////////////////////////////////////////////////////////////////// Construction/destruction//////////////////////////////////////////////////////////////////////CMixer::CMixer () {m_volrange = 100;} Cmixer::~cmixer () {} cmixer::cmixer (const int volrange) {m_volrange = Volrange;}//----------------------------set the volume--- ------------------------------------bool Cmixer::getvolumecontrol (hmixer hmixer, long Componenttype,long Ctrltype, mixercontrol* mxc) {mixerlinecontrols mxlc; Mixerline MXL; BOOL exist = FALSE; mxl.cbstruct = sizeof (MXL); Mxl.dwcomponenttype = ComponentType; if (ComponentType = = Mixerline_componenttype_src_microphone) {//Get recording microphone device Mxl.dwcomponenttype = Mixerline_ Componenttype_dst_wavein; Gets the number of connections in the recording bus Mixergetlineinfo ((hmixerobj) hmixer, &AMP;MXL, Mixer_objectf_hmixer | Mixer_getlineinfof_componenttype); Save the number of connections DWORD dwconnections = mxl.cconnections; Ready to get the microphone device ID DWORD Dwlineid = 0; Esult Mr = Mixergetlineinfo (hmixerobj) hmixer, &AMP;MXL, Mixer_objectf_hmixer | Mixer_getlineinfof_source); Judgment function Execution Error if (Mr!= 0) {break;}//If the current device type is a microphone, jump out of the loop. if (Mxl.dwcomponenttype = = Mixerline_componenttype_src_microphone) {exist = true; break;}} }else if (!mixergetlineinfo (hmixerobj) hmixer, &AMP;MXL, Mixer_getlineinfof_componenttype)) {exist = true;} if (exist) { mxlc.cbstruct = sizeof (MXLC); Mxlc.dwlineid = Mxl.dwlineid; Mxlc.dwcontroltype = Ctrltype; Mxlc.ccontrols = 1; Mxlc.cbmxctrl = sizeof (Mixercontrol); Mxlc.pamxctrl = MXC; if (Mixergetlinecontrols (hmixerobj) hmixer,&mxlc,mixer_getlinecontrolsf_onebytype) return 0; else return 1; return 0; }//---------------------------------------------------------------------------long Cmixer::getmutevalue (hmixer Hmixer, Mixercontrol *mxc) {mixercontroldetails mxcd; MIXERControldetails_boolean Mxcdmute; Mxcd.hwndowner = 0; mxcd.cbstruct = sizeof (MXCD); Mxcd.dwcontrolid = mxc->dwcontrolid; mxcd.cbdetails = sizeof (Mxcdmute); Mxcd.padetails = &mxcdMute; Mxcd.cchannels = 1; Mxcd.cmultipleitems = 0; if (Mixergetcontroldetails (hmixerobj) hmixer, &mxcd,mixer_objectf_hmixer| Mixer_getcontroldetailsf_value)) return-1; return mxcdmute.fvalue; }//---------------------------------------------------------------------------unsigned cmixer::getvolumevalue ( Hmixer hmixer, Mixercontrol *mxc) {mixercontroldetails mxcd; Mixercontroldetails_unsigned Vol; vol.dwvalue=0; Mxcd.hwndowner = 0; mxcd.cbstruct = sizeof (MXCD); Mxcd.dwcontrolid = mxc->dwcontrolid; mxcd.cbdetails = sizeof (vol.); Mxcd.padetails = &vol; Mxcd.cchannels = 1; if (Mixergetcontroldetails (hmixerobj) hmixer, &mxcd, mixer_objectf_hmixer| Mixer_getcontroldetailsf_value)) return-1; return vol.dwvalue; }//---------------------------------------------------------------------------bool CmixEr::setmutevalue (Hmixer hmixer, Mixercontrol *MXC, bool mute) {mixercontroldetails mxcd; Mixercontroldetails_boolean Mxcdmute; Mxcdmute.fvalue=mute; Mxcd.hwndowner = 0; Mxcd.dwcontrolid = mxc->dwcontrolid; mxcd.cbstruct = sizeof (MXCD); mxcd.cbdetails = sizeof (Mxcdmute); Mxcd.padetails = &mxcdMute; Mxcd.cchannels = 1; Mxcd.cmultipleitems = 0; if (Mixersetcontroldetails (hmixerobj) hmixer, &mxcd, mixer_objectf_hmixer| Mixer_setcontroldetailsf_value)) return 0; return 1; }//---------------------------------------------------------------------------bool Cmixer::setvolumevalue ( Hmixer hmixer, Mixercontrol *mxc, long volume) {mixercontroldetails mxcd; Mixercontroldetails_unsigned Vol; Vol.dwvalue = volume; Mxcd.hwndowner = 0; Mxcd.dwcontrolid = mxc->dwcontrolid; mxcd.cbstruct = sizeof (MXCD); mxcd.cbdetails = sizeof (vol.); Mxcd.padetails = &vol; Mxcd.cchannels = 1; if (Mixersetcontroldetails (hmixerobj) hmixer, &mxcd, mixer_objectf_hmixer| Mixer_setcontroldetailsf_value)) REturn 0; return 1; }//---------------------------------------------------------------------------unsigned/*winapi */cmixer:: Getvolume (Mixerdeice Dev)/Get the volume of the device dev=0 main volume, 1WAVE, 2MIDI, 3 line in {long device; unsigned rt=0; Mixercontrol Volctrl; Hmixer Hmixer; Switch (Dev) {case waveout:device=mixerline_componenttype_src_waveout; synthesizer:device=mixerline_ Componenttype_src_synthesizer; Break Case Microphone://Device=mixerline_componenttype_src_compactdisc; Break CD volume Device=mixerline_componenttype_src_microphone; Break Microphone volume//device=mixerline_componenttype_src_line; Break PC speaker volume//device=mixerline_componenttype_src_compactdisc; Break Default:device=mixerline_componenttype_dst_speakers; } if (Mixeropen (&hmixer, 0, 0, 0, 0)) return 0; if (! Getvolumecontrol (Hmixer,device,mixercontrol_controltype_volume,&volctrl)) {mixerclose (hmixer); return 0;} rt= Getvolumevalue (Hmixer,&volctrl) *m_volrange/volctrl.bounds.lmaximum; Mixerclose (Hmixer); RetuRN RT; }//---------------------------------------------------------------------------bool/*winapi*/cmixer::setvolume ( Mixerdeice Dev,long Vol)/Set the volume of the device {//dev =0,1,2 represents the main volume, waveform, MIDI, line in//Vol=0-m_volrange to indicate the volume size, and to set the value of the volume to be returned with a percentage, i.e. The volume from the 0-m_volrange, not the absolute value of the device//Retrun False indicates that the volume size of the operation was unsuccessful//Retrun True indicates that the volume was set to a successful operation long device; BOOL Rc=false; Mixercontrol Volctrl; Hmixer Hmixer; Switch (Dev) {case waveout:device=mixerline_componenttype_src_waveout; synthesizer:device=mixerline_ Componenttype_src_synthesizer; Break Case Microphone://Device=mixerline_componenttype_src_compactdisc; Break CD volume Device=mixerline_componenttype_src_microphone; Break Microphone volume//device=mixerline_componenttype_src_line; Break PC speaker volume//device=mixerline_componenttype_src_compactdisc; Break Default:device=mixerline_componenttype_dst_speakers; } if (Mixeropen (&hmixer, 0, 0, 0, 0)) return 0; if (Getvolumecontrol (Hmixer,device,mixercontrol_controltype_volume,&volctrl) {vol=vol*volctrl.bounds.lmaximum/m_volrange; if (Setvolumevalue (Hmixer,&volctrl,vol)) Rc=true;} mixerClose ( Hmixer); return RC; }//---------------------------------------------------------------------------bool/*winapi*/Cmixer::setmute ( Mixerdeice Dev,/*long*/bool Vol)/set device Mute {//dev =0,1,2 to represent main volume, waveform, MIDI, line in//vol=0,1 respectively to suppress mute, set mute//Retrun false Indicates that canceling or setting the mute operation unsuccessfully//Retrun true means canceling or setting the mute operation successful long device; BOOL Rc=false; Mixercontrol Volctrl; Hmixer Hmixer; Switch (Dev) {case waveout:device=mixerline_componenttype_src_waveout; synthesizer:device=mixerline_ Componenttype_src_synthesizer; Break Case Microphone://Device=mixerline_componenttype_src_compactdisc; Break CD volume Device=mixerline_componenttype_src_microphone; Break Microphone volume//device=mixerline_componenttype_src_line; Break PC speaker volume//device=mixerline_componenttype_src_compactdisc; Break Default:device=mixerline_componenttype_dst_speakers; } if (Mixeropen (&hmixer, 0, 0, 0, 0)) return0; if (Getvolumecontrol (Hmixer,device,mixercontrol_controltype_mute,&volctrl)) if Setmutevalue (hmixer,& Volctrl, (BOOL) vol.) Rc=true; Mixerclose (Hmixer); return RC; }//---------------------------------------------------------------------------bool/*winapi */cmixer::getmute ( Mixerdeice dev)//Check whether the device is muted {//dev =0,1,2 respectively indicates the master volume, waveform, MIDI, line in//Retrun false indicates no mute//retrun True for mute long device; BOOL Rc=false; Mixercontrol Volctrl; Hmixer Hmixer; Switch (Dev) {case waveout:device=mixerline_componenttype_src_waveout; synthesizer:device=mixerline_ Componenttype_src_synthesizer; Break Case Microphone://Device=mixerline_componenttype_src_compactdisc; Break CD volume//device=mixerline_componenttype_src_microphone; Break Microphone volume//device=mixerline_componenttype_src_line; Break PC speaker volume//device=mixerline_componenttype_src_compactdisc; Break Device=mixerline_componenttype_dst_wavein; Break Default:device=mixerline_componenttype_dst_speakers; } if (Mixeropen &Hmixer, 0, 0, 0, 0)) return 0; if (Getvolumecontrol (Hmixer,device,mixercontrol_controltype_mute,&volctrl)) Rc=getmutevalue (hmixer,& Volctrl); Mixerclose (Hmixer); return RC; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.