⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cdplayer.cpp

📁 CD播放器源代码 .rar
💻 CPP
字号:
#include "cdplayer.h"

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <mmsystem.h>

bool			cdValid = false;
bool			playing = false;
bool			wasPlaying = false;
bool			initialized = false;
bool			enabled = false;
bool			playLooping = false;
float			cdvolume;
int				trackLength;
int	 			remap[100];
int				cdrom;
int				playTrack;
int				maxTrack;
int				pzMin;
int				pzSec;

UINT			wDeviceID;

extern HWND		g_hWnd;
unsigned int	nCurrentDevice;

int CDAudio_GetTrackLength()
{
	return trackLength;
}

int CDAudio_GetNumTracks()
{
	return maxTrack;
}

void CDAudio_SetCDValidity(bool Validate)
{
	cdValid = Validate;
}

void CDAudio_SetCallBackWin(HWND hWnd)
{
	g_hWnd = hWnd;
	return;
}

void CDAudio_SetCurrectDevice(unsigned int CurrectDevice)
{
	nCurrentDevice = CurrectDevice;
	return;
}

unsigned int CDGetTrackLength(int Track, TCHAR pzStr[30])
{
	MCI_SET_PARMS		sMCISet;
	MCI_STATUS_PARMS	sMCIStatus;

	sMCISet.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
	mciSendCommand (wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD) (LPVOID) &sMCISet);

	sMCIStatus.dwItem = MCI_STATUS_LENGTH;
	sMCIStatus.dwTrack = Track;
	mciSendCommand (wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &sMCIStatus);

	sMCIStatus.dwReturn /= 1000;

	pzMin = sMCIStatus.dwReturn/60;
	pzSec = sMCIStatus.dwReturn%60;

	if(pzSec < 10)
	{
		wsprintf(pzStr, "Time:  %d:0%d", pzMin, pzSec);
	}
	else
	{
		wsprintf(pzStr, "Time:  %d:%d", pzMin, pzSec);
	}

	sMCISet.dwTimeFormat = MCI_FORMAT_TMSF;
	mciSendCommand (wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD) (LPVOID) &sMCISet);

	return sMCIStatus.dwReturn;
}

void CDAudio_Eject(void)
{
	DWORD	dwReturn;

    if (dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, (DWORD)NULL))
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(NULL, zError, "Error", NULL);
	}
}

void CDAudio_CloseDoor(void)
{
	DWORD	dwReturn;

    if (dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_DOOR_CLOSED, (DWORD)NULL))
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(NULL, zError, "Error", NULL);
	}
}

int CDAudio_GetAudioDiskInfo(void)
{
	DWORD				dwReturn;
	MCI_STATUS_PARMS	mciStatusParms;


	cdValid = false;

	mciStatusParms.dwItem = MCI_STATUS_READY;
    dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
	if (dwReturn)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(NULL, zError, "Error", NULL);
		return -1;
	}
	if (!mciStatusParms.dwReturn)
	{
		return -1;
	}

	mciStatusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
    dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
	if (dwReturn)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(NULL, zError, "Error", NULL);
		return -1;
	}
	if (mciStatusParms.dwReturn < 1)
	{
		return -1;
	}

	cdValid = true;
	maxTrack = mciStatusParms.dwReturn;

	return 0;
}

LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (lParam != wDeviceID)
		return 1;

	switch (wParam)
	{
		case MCI_NOTIFY_SUCCESSFUL:
			if (playing)
			{
				playing = false;
				if (playLooping)
					CDAudio_Play(playTrack, 0, 0, true, hWnd);
			}
			break;

		case MCI_NOTIFY_ABORTED:
		case MCI_NOTIFY_SUPERSEDED:
			break;

		case MCI_NOTIFY_FAILURE:
			CDAudio_Stop ();
			cdValid = false;
			break;

		default:
			return 1;
	}

	return 0;
}

int CDAudio_CheckValidity(char* error)
{
	DWORD				dwReturn;
	MCI_STATUS_PARMS	mciStatusParms;

	mciStatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
	mciStatusParms.dwTrack = 1;
    dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
	if (dwReturn)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		error = zError;
		return 2;
	}
	if (mciStatusParms.dwReturn != MCI_CDA_TRACK_AUDIO)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		error = zError;
		return 1;
	}

	return 0;
}

int CDAudio_Play(int track, int Min, int Sec, bool looping, HWND hWnd)
{
	DWORD				dwReturn;
    MCI_PLAY_PARMS		mciPlayParms;
	MCI_STATUS_PARMS	mciStatusParms;
	
	if (!enabled)
	{
		MessageBox(g_hWnd, "CD-ROM not yet initialized", "Error", NULL);
		return 1;
	}

	CDAudio_GetAudioDiskInfo();
	
	if (!cdValid)
	{
		CDAudio_GetAudioDiskInfo();
		if (!cdValid)
		{
			MessageBox(hWnd, "Invalid CD", "Error", NULL);
			return 1;
		}
	}

	track = remap[track];

	// don't try to play a non-audio track
	mciStatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
	mciStatusParms.dwTrack = track;
    dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
	if (dwReturn)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(hWnd, zError, "Error", NULL);
		return 1;
	}
	if (mciStatusParms.dwReturn != MCI_CDA_TRACK_AUDIO)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(hWnd, zError, "Error", NULL);
		return 2;
	}

	// get the length of the track to be played
	mciStatusParms.dwItem = MCI_STATUS_LENGTH;
	mciStatusParms.dwTrack = track;
    dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
	if (dwReturn)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(hWnd, zError, "Error", NULL);
		return 1;
	}

	trackLength = mciStatusParms.dwReturn;

    mciPlayParms.dwFrom = MCI_MAKE_TMSF(track, Min, Sec, 0);
	mciPlayParms.dwTo = (mciStatusParms.dwReturn << 8) | track;
    mciPlayParms.dwCallback = (DWORD)hWnd;
    dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY | MCI_FROM | MCI_TO, (DWORD)(LPVOID) &mciPlayParms);
	if (dwReturn)
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(hWnd, zError, "Error", NULL);
		return 1;
	}

	playLooping = looping;
	playTrack = track;
	playing = true;
	return 0;
}

void CDAudio_Stop(void)
{
	DWORD	dwReturn;

	if (!enabled)
		return;
	
	if (!playing)
		return;

    if (dwReturn = mciSendCommand(wDeviceID, MCI_STOP, 0, (DWORD)NULL))

	wasPlaying = false;
	playing = false;
}

void CDAudio_Pause(void)
{
	DWORD				dwReturn;
	MCI_GENERIC_PARMS	mciGenericParms;

	if (!enabled)
		return;

	if (!playing)
		return;

	mciGenericParms.dwCallback = (DWORD)g_hWnd;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID) &mciGenericParms))

	wasPlaying = playing;
	playing = false;
}

void CDAudio_Update(void)
{
	if (!enabled)
		return;
}

int CDAudio_Init(int CurrentDevice)
{
	DWORD	dwReturn;
	MCI_OPEN_PARMS	mciOpenParms;
    MCI_SET_PARMS	mciSetParms;
	int				n;

	char zDevice[4];
    sprintf(zDevice, "%c:", (char)CurrentDevice + 'A');
	nCurrentDevice = CurrentDevice;

	mciOpenParms.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
	mciOpenParms.lpstrElementName = zDevice;
	if (dwReturn = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_SHAREABLE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT, (DWORD) (LPVOID) &mciOpenParms))
	{
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(NULL, zError, "Error", NULL);
		return -1;
	}
	wDeviceID = mciOpenParms.wDeviceID;

    // Set the time format to track/minute/second/frame (TMSF).
    mciSetParms.dwTimeFormat = MCI_FORMAT_TMSF;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD)(LPVOID) &mciSetParms))
    {
		char zError[255];
		mciGetErrorString(dwReturn, zError, 255);
		MessageBox(NULL, zError, "Error", NULL);
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, (DWORD)NULL);
		return -1;
    }

	for (n = 0; n < 100; n++)
		remap[n] = n;
	initialized = true;
	enabled = true;

	if (CDAudio_GetAudioDiskInfo())
	{
		cdValid = false;
	}

	return 0;
}

void CDAudio_Shutdown(void)
{
	if (!initialized)
		return;
	CDAudio_Stop();
	if (mciSendCommand(wDeviceID, MCI_CLOSE, MCI_WAIT, (DWORD)NULL))
	{}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -