📄 xaplayer.cpp
字号:
enabled = TRUE;
break;
case XA_INPUT_STATE_CLOSED:
// disable the play and stop buttons, and the slider
enabled = FALSE;
break;
}
// enable/disable the play and stop buttons
SendMessage(m_TrackCommandBar, TB_ENABLEBUTTON, IDM_PLAY, enabled);
SendMessage(m_TrackCommandBar, TB_ENABLEBUTTON, IDM_STOP, enabled);
SendMessage(m_TrackCommandBar, TB_ENABLEBUTTON, IDM_PAUSE, enabled);
// enable/disable the track slider
EnableWindow(m_TrackSlider, enabled);
// autoplay
if (enabled) Play();
}
/*----------------------------------------------------------------------
| WCEPlayer::OnNotifyInputPosition
+---------------------------------------------------------------------*/
void
WCEPlayer::OnNotifyInputPosition(unsigned long offset, unsigned long range)
{
// show position on the slider
SendMessage(m_TrackSlider, TBM_SETPOS, TRUE, offset);
}
/*----------------------------------------------------------------------
| WCEPlayer::OnNotifyInputTimecode
+---------------------------------------------------------------------*/
void
WCEPlayer::OnNotifyInputTimecode(XA_TimecodeInfo *info)
{
TCHAR label[16];
m_Track.timecode = *info;
swprintf(label, TEXT("%02d:%02d:%02d"),
info->h,
info->m,
info->s);
SendMessage(m_StatusWindow, SB_SETTEXT, 1, (LPARAM)label);
m_TrackView->SetTimecode(info->h, info->m, info->s);
}
/*----------------------------------------------------------------------
| WCEPlayer::OnNotifyStreamDuration
+---------------------------------------------------------------------*/
void
WCEPlayer::OnNotifyStreamDuration(unsigned long duration)
{
/* store duration value */
m_Track.duration = duration;
TCHAR label[16];
XA_TimecodeInfo duration_timecode;
duration_timecode.h = (unsigned char)(duration / 3600);
duration -= 3600*duration_timecode.h;
duration_timecode.m = (unsigned char)(duration/60);
duration -= 60*duration_timecode.m;
duration_timecode.s = (unsigned char)duration;
swprintf(label, TEXT("%02d:%02d:%02d"),
duration_timecode.h,
duration_timecode.m,
duration_timecode.s);
SendMessage(m_StatusWindow, SB_SETTEXT, 2, (LPARAM)label);
m_TrackView->SetDuration(m_Track.duration);
}
/*----------------------------------------------------------------------
| WCEPlayer::OnNotifyStreamParameters
+---------------------------------------------------------------------*/
void
WCEPlayer::OnNotifyStreamParameters(XA_StreamParameters *parameters)
{
m_TrackView->SetFrequency(parameters->frequency);
m_TrackView->SetBitrate(parameters->bitrate);
m_TrackView->SetChannels(parameters->nb_channels);
}
/*----------------------------------------------------------------------
| WCEPlayer::OnNotifyOutputState
+---------------------------------------------------------------------*/
void
WCEPlayer::OnNotifyOutputState(XA_OutputState state)
{
BOOL enabled;
switch (state) {
case XA_OUTPUT_STATE_OPEN:
// enable the volume slider
enabled = TRUE;
break;
case XA_OUTPUT_STATE_CLOSED:
// disable the volume slider
enabled = FALSE;
break;
}
}
/*----------------------------------------------------------------------
| WCEPlayer::OnNotifyOutputPcmLevel
+---------------------------------------------------------------------*/
void
WCEPlayer::OnNotifyOutputPcmLevel(unsigned char level)
{
m_Volume = level;
}
/*----------------------------------------------------------------------
| WndProc
+---------------------------------------------------------------------*/
LRESULT CALLBACK
WndProc(HWND window, UINT msg, WPARAM wp, LPARAM lp)
{
LRESULT result = TRUE;
switch (msg) {
// Create Commandbar with buttons from bitmap IDB_STD_SMALL_COLOR.
case WM_CREATE:
Player->InitUserInterface(window);
break;
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(wp,lp)) {
// Buttons
case IDM_PLAY:
if (Player->m_State == XA_PLAYER_STATE_PLAYING) {
Player->Pause();
} else {
Player->Play();
}
break;
case IDM_PAUSE:
Player->Pause();
break;
case IDM_STOP:
Player->Stop();
Player->Seek(0,1);
break;
case IDM_MONO:
Player->SetOutputChannels(XA_OUTPUT_MONO_LEFT);
break;
case IDM_STEREO:
Player->SetOutputChannels(XA_OUTPUT_STEREO);
break;
// File menu
case IDM_EXIT:
SendMessage(window, WM_CLOSE, 0, 0);
break;
case IDM_FILE_OPEN_FILE:
Player->m_ListView->DoOpenFile();
break;
case IDM_FILE_OPEN_PLAYLIST:
Player->m_ListView->DoOpenPlayList();
break;
case IDM_FILE_ADD_FILE:
Player->m_ListView->DoAddFile();
break;
case IDM_FILE_ADD_PLAYLIST:
Player->m_ListView->DoAddPlayList();
break;
case IDM_FILE_SAVE_PLAYLIST:
Player->m_ListView->DoSave();
break;
// Options menu
case IDM_OPTIONS_PREFERENCES:
Player->m_Options.DoDialog(Player->m_Instance, window);
break;
case IDM_OPTIONS_VOLUME:
ShowVolumeDialog(Player->m_Instance, window);
break;
case IDM_OPTIONS_PERF_TESTS:
Player->InputClose(); // close the input, we may test on the same file
DoPerformanceTests(Player->m_Instance, window);
break;
// Help menu
case IDM_HELP_ABOUT:
ShowAboutDialog(Player->m_Instance, window);
break;
// volume popup
case IDM_VOLUME_000: Player->SetVolume( 0); break;
case IDM_VOLUME_010: Player->SetVolume( 10); break;
case IDM_VOLUME_020: Player->SetVolume( 20); break;
case IDM_VOLUME_030: Player->SetVolume( 30); break;
case IDM_VOLUME_040: Player->SetVolume( 40); break;
case IDM_VOLUME_050: Player->SetVolume( 50); break;
case IDM_VOLUME_060: Player->SetVolume( 60); break;
case IDM_VOLUME_070: Player->SetVolume( 70); break;
case IDM_VOLUME_080: Player->SetVolume( 80); break;
case IDM_VOLUME_090: Player->SetVolume( 90); break;
case IDM_VOLUME_100: Player->SetVolume( 100); break;
default:
return DefWindowProc(window, msg, wp, lp);
}
break;
case WM_NOTIFY: {
LPNMHDR header;
header = (LPNMHDR)lp;
if (header->code == TBN_DROPDOWN) {
LPNMTOOLBAR toolbar;
RECT bounds;
HMENU menu;
TPMPARAMS tpm;
toolbar = (LPNMTOOLBAR)lp;
// get click coordinates
SendMessage(header->hwndFrom, TB_GETRECT,
toolbar->iItem, (LPARAM) &bounds);
MapWindowPoints(header->hwndFrom, HWND_DESKTOP,
(LPPOINT)&bounds, 2);
// prevent menu from covering the button
tpm.cbSize = sizeof(tpm);
CopyRect(&tpm.rcExclude, &bounds);
// load menu from resource
menu = GetSubMenu(LoadMenu(Player->m_Instance, ID(IDM_VOLUME_MENU)), 0);
// track the menu
TrackPopupMenuEx(menu, TPM_LEFTALIGN | TPM_VERTICAL,
bounds.left, bounds.bottom, window, &tpm);
}
}
return 0;
case WM_KEYDOWN:
switch (wp) {
case VK_UP:
Player->SetVolume(Player->m_Volume + 5);
break;
case VK_DOWN:
Player->SetVolume(Player->m_Volume - 5);
break;
}
break;
case WM_CLOSE:
DestroyWindow(window);
break;
case WM_DESTROY:
Player->Stop();
delete Player;
PostQuitMessage(0);
break;
default:
result = DefWindowProc(window, msg, wp, lp);
break;
}
return result;
}
/*----------------------------------------------------------------------
| InitInstance
+---------------------------------------------------------------------*/
static BOOL
InitInstance(HINSTANCE instance, int show)
{
HWND window;
// create an Xaudio Player
Player = new WCEPlayer(instance);
// mono by default
Player->SetOutputChannels(XA_OUTPUT_MONO_LEFT);
// close on stop
Player->SetPlayerMode(XA_PLAYER_MODE_OUTPUT_AUTO_CLOSE_ON_STOP);
// set quality from saved preferences
Player->SetCodecQuality(Player->m_Options.m_Codec.quality);
// set device from saved preferences
Player->SetOutputDeviceFromOptions();
// create and show the window
window = CreateWindow(AppName,
AppTitle,
WS_VISIBLE,
0,0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, NULL, instance, NULL);
if (!window) {
return FALSE;
}
// assign an icon for the window
HICON icon = (HICON)SendMessage(window, WM_GETICON, FALSE, 0);
if (icon == (HICON)0) {
icon = (HICON)LoadImage(instance, ID(IDI_XAUDIO_ICON),
IMAGE_ICON,
16, 16, 0);
SendMessage(window, WM_SETICON, FALSE, (LPARAM)icon);
}
// show the window on screen
ShowWindow(window, show);
SetForegroundWindow(window);
UpdateWindow(window);
return TRUE;
}
/*----------------------------------------------------------------------
| InitApplication
+---------------------------------------------------------------------*/
static BOOL
InitApplication(HINSTANCE instance)
{
WNDCLASS wc;
// register our window class
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = 0;
wc.hInstance = instance;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = AppName;
if (RegisterClass(&wc) == FALSE) return FALSE;
return TRUE;
}
/*----------------------------------------------------------------------
| CheckForOtherInstance
+---------------------------------------------------------------------*/
static BOOL
CheckForOtherInstance(TCHAR *class_name, TCHAR *cmdline)
{
CreateMutex(NULL, TRUE, class_name);
if (GetLastError() == ERROR_ALREADY_EXISTS) {
// there is an other instance running
// find it's window, activate it
HWND previous = FindWindow(class_name, NULL);
ShowWindow(previous, SW_SHOW);
SetForegroundWindow(previous);
return TRUE;
}
return FALSE;
}
/*----------------------------------------------------------------------
| WinMain
+---------------------------------------------------------------------*/
int
WINAPI WinMain(HINSTANCE instance,
HINSTANCE previous_instance,
LPWSTR cmdline,
int show)
{
MSG msg;
// load strings
LoadString(instance, IDS_APPNAME, AppName,sizeof(AppName)/sizeof(TCHAR));
LoadString(instance, IDS_APPNAME, AppTitle,sizeof(AppTitle)/sizeof(TCHAR));
// check if xaudio is already running
if (CheckForOtherInstance(AppName, cmdline)) {
return 0;
}
// init the app
if (!previous_instance) {
if ( !InitApplication(instance)) {
return (FALSE);
}
}
// init the instance
if (!InitInstance(instance, show)) {
return (FALSE);
}
while (GetMessage(&msg, NULL, 0,0 ) == TRUE) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -