用c++獲取音頻文件的信息 達到異步播放的效果(1)

這幾個月加班都懵逼了  好久沒有寫博客了

這幾天要做一個可即時打斷的語音端 要想有暫停 續播這樣的功能 我用的mciSendString 異步播放來實現打斷的效果

其中想要獲取音頻文件的播放時長 找了好久才找到可以用的 哎 真是惱火  各種需求

連續加班三個月 哎 希望明年有個好去處 不過還好 至少遇到個好師傅

#include "parsePlayTime.h"
#import <Shell32.dll> 
parsePlayTime::parsePlayTime()
{
//時長字符串中對應下標其代表的秒數
timeIndex[3] = 600;
timeIndex[4] = 60;
timeIndex[6] = 10;
timeIndex[7] = 1;
}

parsePlayTime::~parsePlayTime()
{
}

int parsePlayTime::parseVoicePlayTime(std::string filePath)
{

std::string fileName, dirName;
dirName = filePath.substr(0, filePath.find_last_of('\\') + 1).c_str();
fileName = filePath.substr(filePath.find_last_of('\\') + 1);
CoInitialize(NULL);
clock_t time;
Shell32::IShellDispatchPtr ptrShell;
ptrShell.CreateInstance(__uuidof(Shell32::Shell));
_variant_t var((short)Shell32::ssfRECENT);

//解析目錄
Shell32::FolderPtr ptrFolder = ptrShell->NameSpace(dirName.c_str());

if (ptrFolder == NULL)

{

return -1;

}
//解析文件

Shell32::FolderItemPtr ptrItem = ptrFolder->ParseName(fileName.c_str());

if (ptrItem == NULL)
{
return -1;
}

std::string timeValue = ptrFolder->GetDetailsOf(_variant_t((IDispatch *)ptrItem), 27);
int timeLength = 0;
for (int i = timeValue.size() - 1; i >= 0; i--)
{
if (timeValue[i] >= '0' && timeValue[i] <= '9')
{
timeLength += (timeValue[i] - '0') * timeIndex[i];
}

}
ptrItem.Release();
ptrFolder.Release();
ptrShell.Release();
CoUninitialize();
//由於精度只能到秒 少於1S的都按1S算
timeLength > 0 ? timeLength : 1;
//最後要換算成毫秒
timeLength *= 1000;
return timeLength;

}

拿到的時長是字符串  又因爲我們的音頻文件最多幾分鐘 所以就隨便寫了個轉換的代碼 

主要是拿播放時長的代碼

代碼也是借鑑了網上一位大神的  碼完代碼 找不到網站了 如果侵犯了您的權益 請聯繫我刪除 先說聲抱歉。 

剛入門的小菜雞 請各位多多指教

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章