C ++ playing sound (learning notes)

Source: Internet
Author: User
Document directory
  • Ii. Play a mav sound
  • Iii. Use the MCI interface to play sound in any format
  • 4. Use the powerful MCIWndCreate () function
  • 5. Use the Windows Media Player Control
  • 6. Playing BEEPS
I. Message sound of the playing System
  BOOL WINAPI MessageBeep( __in  UINT uType);

The description of this function in MSDN is: play a waveform file (that is, the wav format). The waveform sound of each sound type is specified by an entry in the registry.

MessageBeep(MB_OK);Sleep(3000);MessageBeep(MB_ICONWARNING);Sleep(3000);MessageBeep(MB_ICONSTOP);Sleep(3000);MessageBeep(MB_ICONERROR);Sleep(3000);MessageBeep(MB_ICONEXCLAMATION);

Ii. Play a mav sound
BOOL PlaySound(LPCTSTR pszSound,HMODULE hmod,DWORD fdwSound);

For more information about the function, see MSDN.

A simple example is as follows:

PlaySound("C:/love.wav",AfxGetInstanceHandle(), SND_FILENAME|SND_ASYNC );
Iii. Use the MCI interface to play sound in any format
# Include <mmsystem. h> // import the sound header file library # pragma comment (lib, "winmm. lib") // import the sound link library

Use the mciSendString function.

mciSendString("open C:/123.mp3" , NULL, 0, 0);mciSendString("play C:/123.mp3 ", NULL, 0, 0); 

The above two can play 123.mp3 files on the C drive.

The specific mci Programming is to be further studied.

4. Use the powerful MCIWndCreate () function
HWND m_hwndMCI; if (m_hwndMCI! = NULL) // create a MCIWND window {MCIWndDestroy (m_hwndMCI);} int type = 0; // hide the default toolbar 0 table display CString filename = "C:/123.mp3 "; // file name to be played: m_hwndMCI = MCIWndCreate (m_hWnd, AfxGetInstanceHandle (), type, filename); MCIWndPlay (m_hwndMCI );
5. Use the Windows Media Player Control

Use project ---> add to project ---> Components and Controls --> Registered ActiveX Controls ---> Windows Media Player
After the Insert control, 17 classes will be automatically added to our project, which are related to the Windows Media Player control. At the same time, there will be more tools bar in our uidesign.
If a control is generated, Windows Media Player can be dragged to the window.
We can link this Control to a Control variable. In this way, the program uses this variable to set WMPlayer.
For example, if we add the variable name m_ctlWMPlayer, we can use m_ctrWMPlayer.SetUrl ("C:/123.mp3") to play the 123. MP3 file under drive C.
Further application is required!

-

Bytes -------------------------------------------------------------------------------------------------------

22: 11: 00 update
6. Play beeps using the Beep function to play beeps at different frequencies

Bool winapi Beep (_ in DWORD dwFreq, // frequency _ in DWORD dwDuration // duration );

For example:

while(1)Beep(5000,500);

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.