这几个月加班都懵逼了 好久没有写博客了
这几天要做一个可即时打断的语音端 要想有暂停 续播这样的功能 我用的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;
}
拿到的时长是字符串 又因为我们的音频文件最多几分钟 所以就随便写了个转换的代码
主要是拿播放时长的代码
代码也是借鉴了网上一位大神的 码完代码 找不到网站了 如果侵犯了您的权益 请联系我删除 先说声抱歉。
刚入门的小菜鸡 请各位多多指教