Music Player

Hey I need some help I want to make a program that pops up says Hello! and theres a button underneath that, and when you click it a song plays. that simple. Any help?
When you don't have a proper tool such as C++Builder, you'll have to do it by hand in WinAPI:
See CreateWindow for creating the window and the controls:
http://msdn.microsoft.com/en-us/library/dd757160%28VS.85%29.aspx

...and mciSendCommand to play the media file:
http://msdn.microsoft.com/en-us/library/dd757160%28VS.85%29.aspx

Alternatively, you can use a GUI framework such as Qt or wxWidgets.
Wait wah? Im newish to this audio coding stuff. So could you like make a example program that shows how it plays the file? Like a snippet of code? Ill figure out the gui stuff.
Last edited on
closed account (D80DSL3A)
Here is a windows program which makes use of the playSound() function. It will play 1 sound at a time only. It is set up for keyboard input.
You will need to place a folder for the sound files in the same folder as the .exe file ( so 2 folders may be needed - one in the debug folder and one in the release folder, if you are generating release versions ). Of course, you will need to place your own .wav files in this folder and then modify the file names used in the program to match those in your AudioClips folder (or whatever you named this folder). If this doesn't work then place a copy of the sounds folder everywhere, until it works, then narrow the right location down (I had to do this).
I built this using Visual C++ 2008 Express Edition and found that it works there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// playSoundShell.cpp
// basic playSound functions included here

#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#pragma comment(lib, "winmm.lib")

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

// Colors, brushes and fonts used.
COLORREF AquaBlue = RGB(51,255,204), yellow = RGB(255,255,102);
HBRUSH   AquaBluebr = CreateSolidBrush(AquaBlue);

HFONT sysfont=static_cast<HFONT>(GetStockObject(SYSTEM_FONT));

// sound clips used
LPCTSTR soundA = L"AudioClips\\GameOpen.wav";
LPCTSTR soundB = L"AudioClips\\gOneShot.wav";
LPCTSTR soundC = L"AudioClips\\rocketFire.wav";
LPCTSTR soundD = L"AudioClips\\airWrench.wav";
LPCTSTR soundE = L"AudioClips\\levelBK1.wav";
LPCTSTR soundF = L"AudioClips\\levelBK2.wav";
LPCTSTR soundG = L"AudioClips\\levelBK3.wav";

HDC hdc;
static HINSTANCE hInst;

int APIENTRY WinMain(HINSTANCE hinst,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
		HWND hwnd;
	MSG  lpmsg;
	WNDCLASSEX wc = {0};
	static wchar_t szAppName[] = L"playSoundShell.app";
	
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.lpszClassName = szAppName;
	wc.hInstance = hinst;
	wc.lpfnWndProc = WndProc;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
	wc.lpszMenuName = 0;
	wc.hbrBackground =  AquaBluebr;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	if( !RegisterClassEx (&wc) )
		return 0;

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,szAppName,L"playSoundShell",
		WS_OVERLAPPEDWINDOW,200,200,640,// started at 600
		480,NULL,NULL,hinst,NULL );

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&lpmsg,NULL,0,0) )
	{
		TranslateMessage(&lpmsg);
		DispatchMessage(&lpmsg);
	}//End Message Loop.

	return(lpmsg.wParam);
 	// TODO: Place code here.
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{	
	PAINTSTRUCT ps;
	
	static POINTS pt,sz={300,300};
	RECT rect;
	char ch;
	int j=0;// for looping.
	
	switch(msg)
	{
		case WM_CREATE:			
			hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
			return 0 ;

		case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);
			ValidateRect(hwnd, NULL);
			GetClientRect (hwnd, &rect) ;
			TextOut(hdc, 100, 100, L"This is a project shell with working playSound calls", 52);
			EndPaint(hwnd, &ps);			
			return 0;
			
		case WM_CHAR:// Handles keyboard events.			
			ch = wParam;
			ch = tolower(ch);

			switch( ch )
			{
				case 'a':// non-looping clips
					PlaySound((LPCTSTR) soundA, NULL, SND_FILENAME | SND_ASYNC);
					break;
				case 'b':
					PlaySound((LPCTSTR) soundB, NULL, SND_FILENAME | SND_ASYNC);
					break;
				case 'c':
					PlaySound((LPCTSTR) soundC, NULL, SND_FILENAME | SND_ASYNC);
					break;
				case 'd':
					PlaySound((LPCTSTR) soundD, NULL, SND_FILENAME | SND_ASYNC);
					break;
				case 'e':// looping clips
					PlaySound((LPCTSTR) soundE, NULL, SND_LOOP | SND_FILENAME | SND_ASYNC);
					break;
				case 'f':
					PlaySound((LPCTSTR) soundF, NULL, SND_LOOP | SND_FILENAME | SND_ASYNC);
					break;
				case 'g':
					PlaySound((LPCTSTR) soundG, NULL, SND_LOOP | SND_FILENAME | SND_ASYNC);
					break;
				case 's':
					PlaySound(NULL, 0, 0);// stops playback of clip
					break;
				case VK_ESCAPE:// terminate app.					
					PostQuitMessage(0);
					break;
				default:
					break;
			}
			return 0;		

		case WM_DESTROY:			
			PostQuitMessage(0);
			break;		
		default:
			return( DefWindowProc(hwnd, msg, wParam, lParam) );
			break;
	}//End Switch on msg.
	return(0);
}//End CALLBACK. 

Last edited on
Thanks but I was hoping for a button to click on to play the file. And how does this play the sound file. automaticaly?
if you want a button use a resource file
Last edited on
Thanks. I guess Ill just use this code and mess it up a bit.
closed account (D80DSL3A)
It plays the sound files when a key is struck on the keyboard. As written, entering a,b,c,d,e,f or g will make different sounds play. You said you could figure out the "gui stuff" so I posted some working code I had in a project.

Besides, you wouldn't want my code for the use of a button...it's kinda unconventional.

I posted this because someone did the same for me when I was first looking to use sound in my programs. I'm not sure I would have got it otherwise since it has to be exactly right to work at all.
Ok. Thanks Ill put in the button myself.
You could use the OpenAL library, but it may be more complicated for this simple example. I suggest using fun2code's example and editing it to your needs.
Topic archived. No new replies allowed.