循环播放一个mid格式的音乐文件,在线等,马上结贴
mciSendCommand()这个函数怎么用,哪位大哥能给一个简单的代码例子,msdn上找不到呀,我只想用它循环播放一个mid格式的音乐文件.
[解决办法]
// Plays a specified MIDI file by using MCI_OPEN and MCI_PLAY. Returns
// as soon as playback begins. The window procedure function for the
// specified window will be notified when playback is complete.
// Returns 0L on success; otherwise, it returns an MCI error code.
DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName)
{
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_PLAY_PARMS mciPlayParms;
MCI_STATUS_PARMS mciStatusParms;
MCI_SEQ_SET_PARMS mciSeqSetParms;
// Open the device by specifying the device and filename.
// MCI will attempt to choose the MIDI mapper as the output port.
mciOpenParms.lpstrDeviceType = "sequencer ";
mciOpenParms.lpstrElementName = lpszMIDIFileName;
if (dwReturn = mciSendCommand(NULL, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
(DWORD)(LPVOID) &mciOpenParms))
{
// Failed to open device. Don 't close it; just return error.
return (dwReturn);
}
// The device opened successfully; get the device ID.
wDeviceID = mciOpenParms.wDeviceID;
// Check if the output port is the MIDI mapper.
mciStatusParms.dwItem = MCI_SEQ_STATUS_PORT;
if (dwReturn = mciSendCommand(wDeviceID, MCI_STATUS,
MCI_STATUS_ITEM, (DWORD)(LPVOID) &mciStatusParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
// The output port is not the MIDI mapper.
// Ask if the user wants to continue.
if (LOWORD(mciStatusParms.dwReturn) != MIDI_MAPPER)
{
if (MessageBox(hMainWnd,
"The MIDI mapper is not available. Continue? ",
" ", MB_YESNO) == IDNO)
{
// User does not want to continue. Not an error;
// just close the device and return.
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (0L);
}
}
// Begin playback. The window procedure function for the parent
// window will be notified with an MM_MCINOTIFY message when
// playback is complete. At this time, the window procedure closes
// the device.
mciPlayParms.dwCallback = (DWORD) hWndNotify;
if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
(DWORD)(LPVOID) &mciPlayParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
return (0L);
}
[解决办法]
加个SND_LOOP循环就可以了呀!
假如你想在背景乐前还有其他很多音乐的话,建议你使用VFW,方便简单
可以同时播放N多音乐,下面时以前写的一个mp3播放器的,见笑呵呵!
支持wav,mid,mp3,wma等音频格式,如果音乐播放的时候继续打开,前面的音乐不会听掉,而是叠加在一起,实现你的背景音乐,MCIWnd开头的就是VFW的函数,使用很简单,估计你一看函数名就知道作用了!
void CMiniMp3Dlg::OnBtn1() //打开
{
// TODO: Add your control notification handler code here
m_Audio = NULL;
if(m_Audio == NULL)
{
CFileDialog mp3(TRUE,NULL,NULL,OFN_HIDEREADONLY,"MP3 WMA Files (*.mp3 *.WMA)|*.mp3;*.wma|");
if(mp3.DoModal() == IDOK)
{
m_Path = mp3.GetPathName();
m_Audio = MCIWndCreate(this->GetSafeHwnd(),
AfxGetInstanceHandle(),
WS_CHILD|MCIWNDF_NOMENU,m_Path);
long snd =MCIWndGetLength(m_Audio);//得到文件长度
m_sInfo=TimeFormat(snd);
//m_sInfo.Format("%d",snd);
UpdateData(FALSE);
m_cSliderMusic.SetRangeMin(0);
m_cSliderMusic.SetRangeMax(snd/1000);
}
}
}
void CMiniMp3Dlg::OnBtn2() //播放
{
// TODO: Add your control notification handler code here
MCIWndHome(m_Audio);
MCIWndPlay(m_Audio);
SetTimer(0,1000,NULL);
nP=0;
}
void CMiniMp3Dlg::OnBtn3() //暂停,恢复
{
//long snd =MCIWndGetPosition(m_Audio);
//m_sInfo=TimeFormat(snd);
if (GetMode(m_Audio)==1)
MCIWndPause(m_Audio);
else
if (GetMode(m_Audio)==2)
MCIWndResume(m_Audio);
}
void CMiniMp3Dlg::OnOK() //销毁
{
// TODO: Add extra validation here
MCIWndDestroy(m_Audio);
CDialog::OnOK();
}
void CMiniMp3Dlg::OnBtn4() //停止
{
// TODO: Add your control notification handler code here
MCIWndStop(m_Audio);
}