Skip to content

Commit

Permalink
Added volume control
Browse files Browse the repository at this point in the history
  • Loading branch information
RadAd committed May 15, 2019
1 parent 950d648 commit 0ced7b5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
69 changes: 66 additions & 3 deletions AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ BOOL PlayerDlgOnInitDialog(HWND hDlg, HWND hWndFocus, LPARAM lParam)
SetWindowLongPtr(hDlg, DWLP_USER, (LONG_PTR) pData);

const HWND hPosition = GetDlgItem(hDlg, IDC_POSITION);
const HWND hVolume = GetDlgItem(hDlg, IDC_VOLUME);

GetWindowText(hDlg, pData->strTitle, ARRAYSIZE(pData->strTitle));

Expand All @@ -80,6 +81,11 @@ BOOL PlayerDlgOnInitDialog(HWND hDlg, HWND hWndFocus, LPARAM lParam)
TrackBar_SetTicFreq(hPosition, 60);
TrackBar_SetLineSize(hPosition, 10);
TrackBar_SetPageSize(hPosition, 60);
TrackBar_SetTicFreq(hVolume, 10);
TrackBar_SetLineSize(hVolume, 1);
TrackBar_SetPageSize(hVolume, 10);
TrackBar_SetRange(hVolume, 0, 100);
TrackBar_SetPos(hVolume, 100 - 100);

for (int i = 1; i < __argc; ++i)
{
Expand Down Expand Up @@ -199,6 +205,7 @@ void PlayerDlgOnAPOpen(HWND hDlg, LPCTSTR pFileName)
{
PlayerDlgData* pData = (PlayerDlgData*) GetWindowLongPtr(hDlg, DWLP_USER);
const HWND hPosition = GetDlgItem(hDlg, IDC_POSITION);
const HWND hVolume = GetDlgItem(hDlg, IDC_VOLUME);
const HWND hBegin = GetDlgItem(hDlg, IDC_BEGIN);
const HWND hEnd = GetDlgItem(hDlg, IDC_END);

Expand All @@ -215,6 +222,9 @@ void PlayerDlgOnAPOpen(HWND hDlg, LPCTSTR pFileName)
FormatTime(dwLength, buf, ARRAYSIZE(buf));
SetWindowText(hEnd, buf);

DWORD dwVolume = 100 - TrackBar_GetPos(hVolume);
MciSetVolume(hDlg, pData->wDeviceID, dwVolume * 10);

MciPlay(hDlg, pData->wDeviceID);
}
else
Expand All @@ -235,10 +245,12 @@ BOOL PlayerDlgOnNotify(HWND hDlg, int id, LPNMHDR pNmHdr)
{
PlayerDlgData* pData = (PlayerDlgData*) GetWindowLongPtr(hDlg, DWLP_USER);
const HWND hPosition = GetDlgItem(hDlg, IDC_POSITION);
const HWND hVolume = GetDlgItem(hDlg, IDC_VOLUME);
const HWND hBegin = GetDlgItem(hDlg, IDC_BEGIN);

if (id == IDC_POSITION)
switch (id)
{
case IDC_POSITION:
switch (pNmHdr->code)
{
case NM_RELEASEDCAPTURE:
Expand Down Expand Up @@ -281,14 +293,47 @@ BOOL PlayerDlgOnNotify(HWND hDlg, int id, LPNMHDR pNmHdr)
break;
}
return TRUE;
}
else

case IDC_VOLUME:
switch (pNmHdr->code)
{
case NM_RELEASEDCAPTURE:
if (pData->wDeviceID != 0)
{
DWORD dwVolume = 100 - TrackBar_GetPos(hVolume);
MciSetVolume(hDlg, pData->wDeviceID, dwVolume * 10);
}
break;

case TRBN_THUMBPOSCHANGING:
if (pData->wDeviceID != 0)
{
const NMTRBTHUMBPOSCHANGING* pPosChanging = (NMTRBTHUMBPOSCHANGING*) pNmHdr;
DWORD dwPos = 100 - pPosChanging->dwPos;
if (dwPos > (MAXDWORD / 2)) dwPos = 0;
if (dwPos > 100) dwPos = 100;
if (pPosChanging->nReason == TB_THUMBTRACK)
{
MciSetVolume(hDlg, pData->wDeviceID, dwPos * 10);
}
else if (pPosChanging->nReason != TB_THUMBPOSITION && pPosChanging->nReason != TB_ENDTRACK)
{
MciSetVolume(hDlg, pData->wDeviceID, dwPos * 10);
}
}
break;
}
return TRUE;

default:
return FALSE;
}
}

void PlayerDlgOnAppCommand(HWND hDlg, HWND hChild, UINT cmd, UINT uDevice, DWORD dwKeys)
{
PlayerDlgData* pData = (PlayerDlgData*) GetWindowLongPtr(hDlg, DWLP_USER);
const HWND hVolume = GetDlgItem(hDlg, IDC_VOLUME);
switch (cmd)
{
case APPCOMMAND_MEDIA_PLAY:
Expand All @@ -306,6 +351,24 @@ void PlayerDlgOnAppCommand(HWND hDlg, HWND hChild, UINT cmd, UINT uDevice, DWORD
MciPlay(hDlg, pData->wDeviceID);
}
break;
case APPCOMMAND_VOLUME_DOWN:
{
DWORD dwVolume = 100 - TrackBar_GetPos(hVolume);
dwVolume -= 10;
if (dwVolume > (DWORD) -100) dwVolume = 0;
MciSetVolume(hDlg, pData->wDeviceID, dwVolume * 10);
TrackBar_SetPos(hVolume, 100 - dwVolume);
}
break;
case APPCOMMAND_VOLUME_UP:
{
DWORD dwVolume = 100 - TrackBar_GetPos(hVolume);
dwVolume += 10;
if (dwVolume > 100) dwVolume = 100;
MciSetVolume(hDlg, pData->wDeviceID, dwVolume * 10);
TrackBar_SetPos(hVolume, 100 - dwVolume);
}
break;
}
}

Expand Down
Binary file modified AudioPlayer.rc
Binary file not shown.
23 changes: 22 additions & 1 deletion MciUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <Windows.h>
#include "MciUtils.h"
#include <stdio.h>
#include <tchar.h>

// TODO Use a different alias for each open
#define DEF_ALIAS _T("music")

static void MciShowError(HWND hDlg, MCIERROR e)
{
Expand Down Expand Up @@ -32,7 +37,8 @@ MCIERROR MciOpen(HWND hDlg, MCIDEVICEID& wDeviceID, LPCTSTR pFileName)
MCI_OPEN_PARMS op = {};
op.dwCallback = MAKELONG(hDlg, 0);
op.lpstrElementName = pFileName;
MCIERROR e = mciSendCommand(wDeviceID, MCI_OPEN, MCI_WAIT | MCI_OPEN_ELEMENT, (DWORD_PTR) &op);
op.lpstrAlias = DEF_ALIAS;
MCIERROR e = mciSendCommand(wDeviceID, MCI_OPEN, MCI_WAIT | MCI_OPEN_ELEMENT | MCI_OPEN_ALIAS, (DWORD_PTR) &op);
if (e != MMSYSERR_NOERROR)
{
//MciClose(hDlg, op.wDeviceID);
Expand Down Expand Up @@ -77,3 +83,18 @@ void MciSeekTo(HWND hDlg, MCIDEVICEID wDeviceID, DWORD dwTo)
if (e != MMSYSERR_NOERROR)
MciShowError(hDlg, e);
}

void MciSetVolume(HWND hDlg, MCIDEVICEID wDeviceID, DWORD dwVolume)
{
TCHAR buffer[1024];
_stprintf_s(buffer, ARRAYSIZE(buffer), _T("setaudio %s volume to %d"), DEF_ALIAS, dwVolume);

TCHAR ret[1024];
MCIERROR e = mciSendString(buffer, ret, ARRAYSIZE(ret), hDlg);
if (e != MMSYSERR_NOERROR)
MciShowError(hDlg, e);
#if 0
if (ret[0] != _T('\0'))
_ftprintf(stdout, _T("%s\n"), ret);
#endif
}
1 change: 1 addition & 0 deletions MciUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ DWORD_PTR MciGetStatus(HWND hDlg, MCIDEVICEID wDeviceID, DWORD dwItem);
void MciPlay(HWND hDlg, MCIDEVICEID wDeviceID);
void MciPause(HWND hDlg, MCIDEVICEID wDeviceID);
void MciSeekTo(HWND hDlg, MCIDEVICEID wDeviceID, DWORD dwTo);
void MciSetVolume(HWND hDlg, MCIDEVICEID wDeviceID, DWORD dwVolume);
Binary file modified resource.h
Binary file not shown.

0 comments on commit 0ced7b5

Please sign in to comment.