当文件的路径+文件名很长时,我注意到了
PlaySound(fName.c_str(), NULL, SND_ASYNC);
有效,但无效
mciSendString((L"open \"" + fName + L"\" type waveaudio alias sample").c_str(), NULL, 0, NULL);
mciSendString(L"play sample", NULL, 0, NULL);
失败命令示例:
open "C:\qisdjqldlkjsqdjqdqjslkdjqlksjlkdjqsldjlqjsdjqdksq\dajdjqjdlqjdlkjazejoizajoijoifjoifjdsfjsfszjfoijdsjfoijdsoifoidsjfojdsofjdsoijfoisjfoijoisdjfosjfqsd\Windows Critical Stop.wav" type waveaudio alias sample
但是:
我确实需要 mciSendString 而不是 PlaySound(),因为 PlaySound() 不播放某些文件(48 khz 音频文件,有时是 24 位文件等)
我需要能够播放路径可能很长的音频文件,因为我的应用的最终用户可能拥有此类文件
如何让 mciSendString 接受长文件名?
注意事项:
我还尝试使用 mciSendCommand 来处理这个 MSDN 示例, 但都是一样的。
最大路径+文件名长度为 127(127:工作,128+:不工作)
如果真的不可能使
mci*
函数处理超过 127 个字符的文件名,我可以使用什么来代替,只需使用 winapi (没有外部库)? (PlaySound
不是一个选项,因为它不能真正处理所有 wav 文件,例如 48 khz:不工作等)
最佳答案
127 的限制看起来很奇怪。我没有在 MSDN 上找到任何关于它的信息。
有另一种语法可以打开:
open waveaudio!right.wav
您可以尝试的一个选项是将工作目录更改为文件的目录,然后限制仅适用于文件名。 ->
SetCurrentDiectory
要缩短文件名,可以使用 Winapi 函数
GetShortPathName
但是:SMB 3.0 does not support short names on shares with continuous availability capability.
Resilient File System (ReFS) doesn't support short names. If you call GetShortPathName on a path that doesn't have any short names on-disk, the call will succeed, but will return the long-name path instead. This outcome is also possible with NTFS volumes because there's no guarantee that a short name will exist for a given long name.
基于 MSDN 的示例:
#include <string>
#include <Windows.h>
template<typename StringType>
std::pair<bool, StringType> shortPathName( const StringType& longPathName )
{
// First obtain the size needed by passing NULL and 0.
long length = GetShortPathName( longPathName.c_str(), NULL, 0 );
if (length == 0) return std::make_pair( false, StringType() );
// Dynamically allocate the correct size
// (terminating null char was included in length)
StringType shortName( length, ' ' );
// Now simply call again using same long path.
length = GetShortPathName( longPathName.c_str(), &shortName[ 0 ], length );
if (length == 0) return std::make_pair( false, StringType() );
return std::make_pair(true, shortName);
}
#include <locale>
#include <codecvt>
#include <iostream>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
//std::string narrow = converter.to_bytes( wide_utf16_source_string );
//std::wstring wide = converter.from_bytes( narrow_utf8_source_string );
int main( int argc, char** argv )
{
std::wstring myPath = converter.from_bytes( argv[0] );
auto result = shortPathName( myPath );
if (result.first)
std::wcout << result.second ;
return 0;
}
关于c++ - 如果路径太长,mciSendString 将不会播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221390/