// mp3player2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <Mmsystem.h>
#pragma comment (lib,"winmm.lib")
#include <conio.h> //_getch()
class Mp3Player
{
protected:
DWORD m_idPlayer;
Mp3Player(DWORD id)
{
m_idPlayer = id;
}
public:
//Open_File
//Thong tin:
// - Khoi tao doi tuong Mp3Player
// - Tham so dua vao la duong dan file MP3
static Mp3Player* Open_File(WCHAR *lpFileName)
{
#pragma warning (disable:4311)
// Tham so cho Media
MCI_OPEN_PARMS mciOpenParms;
// Driver de play MP3 (tham khao them trong MSDN)
mciOpenParms.lpstrDeviceType = L"mpegvideo";
// File mp3 se play
mciOpenParms.lpstrElementName = lpFileName;
// Khoi tao mp3Player
DWORD dwResult = mciSendCommand(0, MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD) &mciOpenParms);
// Loi khoi tao (co the do soundCard)
if (dwResult != 0 )
return NULL;
// Thiet lap tham so cho mp3Player (su dung don vi Milisecond)
MCI_SET_PARMS param;
param.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
dwResult = mciSendCommand(mciOpenParms.wDeviceID,MCI_SET,MCI_SET_TIME_FORMAT,(DWORD) (LPMCI_SET_PARMS) ¶m);
// Loi thiet lap tham so
if (dwResult != 0 )
return NULL;
#pragma warning (default:4311)
// Khoi tao doi tuong Mp3
return new Mp3Player( mciOpenParms.wDeviceID );
}
virtual ~Mp3Player()
{
// Huy mp3Player
mciSendCommand(m_idPlayer, MCI_CLOSE, 0, NULL);
}
void Release(){delete this;}
// Play()
// Thong tin: Play file nhac
void Play()
{
#pragma warning (disable:4311)
MCI_PLAY_PARMS param;
param.dwCallback = (DWORD) NULL;
mciSendCommand(m_idPlayer, MCI_PLAY, MCI_NOTIFY,(DWORD)(LPVOID) ¶m);
#pragma warning (default:4311)
}
// Stop()
// Thong tin: Dung file nhac
void Stop()
{
#pragma warning (disable:4311)
MCI_GENERIC_PARMS param;
param.dwCallback = NULL;
mciSendCommand(m_idPlayer,MCI_STOP,MCI_NOTIFY,(DWORD) (LPMCI_GENERIC_PARMS) ¶m);
#pragma warning (default:4311)
}
// Pause()
// Thong tin: Tam dung file nhac
void Pause()
{
#pragma warning (disable:4311)
MCI_GENERIC_PARMS param;
param.dwCallback = NULL;
mciSendCommand(m_idPlayer,MCI_PAUSE,MCI_NOTIFY,(DWORD)(LPMCI_GENERIC_PARMS) ¶m);
#pragma warning (default:4311)
}
// Resume()
// Thong tin: Tiep tuc play (do pause)
void Resume()
{
#pragma warning (disable:4311)
MCI_GENERIC_PARMS param;
param.dwCallback = NULL;
mciSendCommand(m_idPlayer,MCI_RESUME,MCI_NOTIFY,(DWORD)(LPMCI_GENERIC_PARMS) ¶m);
#pragma warning (default:4311)
}
// Length()
// Thong tin: Tra ve thoi gian file mp3 (milisecond)
DWORD Get_Length()
{
#pragma warning (disable:4311)
MCI_STATUS_PARMS param;
param.dwItem = MCI_STATUS_LENGTH;
DWORD dwResult = mciSendCommand(m_idPlayer,MCI_STATUS ,MCI_STATUS_ITEM, (DWORD) (LPMCI_STATUS_PARMS) ¶m);
#pragma warning (default:4311)
if (dwResult != 0)
return false;
return (DWORD)param.dwReturn;
}
// Get_Position()
// Thong tin: Tra ve vi tri (thoi diem milisecond) dang play
DWORD Get_Position()
{
#pragma warning (disable:4311)
MCI_STATUS_PARMS param;
param.dwItem = MCI_STATUS_POSITION;;
DWORD dwResult = mciSendCommand(m_idPlayer,MCI_STATUS ,MCI_STATUS_ITEM, (DWORD) (LPMCI_STATUS_PARMS) ¶m);
#pragma warning (default:4311)
if (dwResult != 0)
return false;
return (DWORD)param.dwReturn;
}
// Seek()
// Thong tin: Nhay den vi tri seekto de play (milisecond)
void Seek(DWORD seekto)
{
#pragma warning (disable:4311)
MCI_SEEK_PARMS param;
param.dwTo = seekto;
mciSendCommand(m_idPlayer,MCI_SEEK,MCI_TO,(DWORD) (LPMCI_SEEK_PARMS) ¶m);
#pragma warning (default:4311)
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Mp3Player *pPlayer = Mp3Player::Open_File(L"aaa.mp3");
if (pPlayer == NULL)
{
printf("Sound card hoac path mp3 khong hop le\n");
return 1;
}
pPlayer->Play();
printf("Hay lang nghe nhe... (press a key to stop)\n");
_getch();
pPlayer->Release();
return 0;
}
Bài đăng phổ biến
-
1.1.1. THỰC HÀNH 4: THIẾT LẬP FORUM SỬ DỤNG PHP VÀ MYSQL Cài đặt MyBB Gói mã nguồn mở được cung cấp hoàn toàn miễn phí tại tran...
-
1.1.1. TẠO WEBSITE CHO NGƯỜI DÙNG Mô hình và yêu cầu bài tập Hướng dẫn thực hiện a. Tạo userdir 1) Khai báo Us...
-
5.4. DỊCH VỤ WEB 5.4.1. GIỚI THIỆU WEB SERVER Apache là một phần mềm có nhiều tính năng mạnh và linh hoạt dùng để làm Web Server. - Hỗ tr...
-
For a small robot project I'm working on I needed a way to measure the robot's progress across the floor. There are various poss...
-
Mathworks Matlab R2012a-ISO Mathworks Matlab R2012a-ISO - MATLAB® is a high-level technical computing language and in...
-
Cài đặt SMARTY LIBRARY Copy các file thư viện Smarty vào /usr/local/lib/php/Smarty/ $> cd YOUR_DOWNLOAD_DIRECTORY $> gtar -zxvf...
-
Phần mềm học anh văn hiệu quả | Longman Dictionary of Contemporary English 5th Edition [Từ điển anh ngữ] Longman Dictionary of Co...
-
How to Convert an OGV File to AVI in Ubuntu February 6 You might have used Istanbul http://goo.gl/aHdeo or Recordmydesktop http:...
-
CÁC BƯỚC THỰC HIỆN Các FR sẽ giữ liên lạc với người phụ trách và được hướng dẫn công việc chủ yếu qua Skype (phần mềm chat tương tự Yahoo Me...
-
Afin d’accélérer un code sans entrer dans une ingénierie trop développée, j’ai décidé d’utiliser OpenMP pour paralléliser des tâches et c...