功能:使用了media player播放器,播放的檔案是自己寫的檔案,main函式中播放了音頻檔案,同時設定500ms的定時器,每次超時時候判斷播放是否完畢,如果完畢,重新寫一次檔案再播放一次,一直這樣重復。(當然,實際工程里是比這復雜得多的程序,為了描述問題,我把功能簡化了一下,可能有點別扭)
問題:在定時器超時查詢到播放完畢后,再寫檔案時,即WriteFile中的代碼DesFile.open總是回傳不正確,有大神知道原因嗎?ex.GetErrorMessage(szError, 1024);這個陳述句告訴我的原因是A sharing violation occurred while accessing D:\\Des.wav.
檔案已經播放完畢還不能再open嗎?
CString DesPathName = _T("D:\\Des.wav");
CString SourcePathName = _T("D:\\Src.wav");
void WriteFile(void)
{
CFile DesFile;
CFile SourceFile;
CFileException ex;
if (!SourceFile.Open(SourcePathName , CFile::modeRead))
return;
if(DesFile.open(DesPathName ,CFile::shareDenyNone | CFile::modeWrite|CFile::modeCreate,&ex))
{
unsigned long size = SourceFile.GetLength();
char *pbody = new char[size];
SourceFile.Read(pbody,size);
DesFile.Write(pbody,size);
DesFile.Flush();
DesFile.Close();
delete []pbody;
}
else
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
}
SourceFile.Close();
}
void PlayAudio(void)
{
WriteFile();
m_player.put_URL(DesPathName);
}
void OnTimer(UINT nIDEvent)
{
if(nIDEvent==TIMER_UPDATE)
{
if(m_player.get_playState()==1)//播放完畢
PlayAudio();
}
}
void main()
{
PlayAudio();
SetTimer(TIMER_UPDATE,500,NULL);
}
uj5u.com熱心網友回復:
你寫的時候,這個檔案是否還存在,或者被播放器占用,你再次寫入就寫失敗了你可以再次播放的時候改個名字
uj5u.com熱心網友回復:
是不是寫入的檔案被占用,導致寫入失敗。uj5u.com熱心網友回復:
應該是被占用了,但是有什么辦法解除占用呢?
uj5u.com熱心網友回復:
在OnTimer里,播放完畢后的代碼里,你再調一下播放器的停止或關閉介面,然后再操作檔案。
uj5u.com熱心網友回復:
A sharing violation occurred while accessing D:\\Des.wav.存取 D:\\Des.wav 是 共享 違規,就是 不能 共享, 寫到 臨時檔案中。
uj5u.com熱心網友回復:
為什么不用m_wave.Replay();
??
//
DWORD CWave::Replay()
{
DWORD dwResult=0;
if(m_nElementID)
{
dwResult=mciSendCommand(m_nElementID, MCI_SEEK,MCI_SEEK_TO_START, NULL);
//display error message if failed
if(dwResult)
{
DisplayErrorMsg(dwResult);
Stop();
}
//
MCI_PLAY_PARMS mciPlayParms;
mciPlayParms.dwCallback=(DWORD)AfxGetMainWnd()->m_hWnd;
//set the window that will receive notification message
//instruct device to play file
dwResult=mciSendCommand(m_nElementID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
//display error and close element if failed
if(dwResult)
{
DisplayErrorMsg(dwResult);
Stop();
}
}
return dwResult;
}
uj5u.com熱心網友回復:
http://download.csdn.net/download/schlafenhamster/719635““用VC6.0 MFC編的播放程式””
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/86866.html
標籤:基礎類
