Write piano applet using C language

  • 2020-06-03 07:39:47
  • OfStack

Search the Internet keyboard piano, you can search a lot of small games, the most often played is Flash small game, 26 buttons.

Later I want to use C language to achieve 1, no interface ~ ~ console.

The principle is very simple, first get the key event in the console, in the key event, open a thread, this thread is dedicated to play the key corresponding sound mp3.

Why use threads? Because if you press the button to play, if you don't finish playing mp3, you won't be able to play the same as 1mp3.

In the thread function, use the mciSendString function to open and play the corresponding mp3 file.

At the beginning, When I finished writing, I was very happy and could play. I found some music from the Internet and played happily. Later, I found a question:

After playing for a period of time, there was no sound on the button again. After several twists and turns, I found a solution to the problem. In the thread function, after playing mp3, Sleep1 for a period of time, and then Close.

Since each key sound is 3 seconds, the Sleep time is set at 3000.

Here's the code:

#include <stdio.h> 
#include <process.h> 
#include <windows.h> 
#include <Mmsystem.h> 
#pragma comment ( lib, "Winmm.lib" ) 
*  Hide console cursor  
void HideTheCursor() 
 if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { 
  cciCursor.bVisible = FALSE; 
  SetConsoleCursorInfo(hStdOut, &cciCursor); 
*  Play the thread  
unsigned _stdcall thread_play(LPVOID lpParam) 
 char sz_command[126] = {0}; 
 char sz_cur_play[32] = {0}; 
 /* Open the mp3 file */ 
 sprintf(sz_command, "open \"key\\%c.mp3\" alias key_%c", (WORD)lpParam, (WORD)lpParam); 
 mciSendString(sz_command, 0, 0, 0 ); 
 sprintf(sz_cur_play, "key_%c", (WORD)lpParam); 
 /* play */ 
 sprintf(sz_command, "play %s", sz_cur_play); 
 mciSendString(sz_command, 0, 0, 0 ); 
 /*3 Seconds after closing */ 
 sprintf(sz_command, "close %s", sz_cur_play); 
 mciSendString(sz_command, 0, 0, 0); 
 return 0; 
 /* Standard input handle */ 
 HWND h_console_in; 
 /* Input record */ 
 INPUT_RECORD input_rec; 
 DWORD res; 
 /* Handle to the thread */ 
 unsigned id_thread_play; 
 printf(" Welcome to keyboard piano!! \n\n"); 
 /* Gets the standard input handle and sets it to accept input */ 
 h_console_in = GetStdHandle(STD_INPUT_HANDLE); 
 SetConsoleMode(h_console_in, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT); 
 while (1) 
  /* read 1 An input */ 
  ReadConsoleInput(h_console_in, &input_rec, 1, &res); 
  /* Flush input buffer */ 
  /* The input event is key input, and when the key is pressed */ 
  if (input_rec.EventType == KEY_EVENT && input_rec.Event.KeyEvent.bKeyDown == TRUE) 
   /* The input is A-Z Buttons between */ 
   if (input_rec.Event.KeyEvent.wVirtualKeyCode >= 'A' 
    && input_rec.Event.KeyEvent.wVirtualKeyCode <= 'Z') 
    /* Opens the thread with the current key as a parameter */ 
    h_thread_play = (HANDLE)_beginthreadex(NULL, 0, &thread_play, 
     (void *)input_rec.Event.KeyEvent.wVirtualKeyCode, 0, &id_thread_play); 

Related articles: