VS C# 2008 SP1
我创建了一个记录和播放音频的小应用程序。但是,我的应用程序需要将波形文件保存到用户计算机上的应用程序数据目录中。
mciSendString 采用 C 样式字符串作为参数,并且必须采用 8.3 格式。但是,我的问题是我无法保存它。奇怪的是有时会,有时不会。然而,大多数时候都是失败的。但是,如果我直接保存到 C 驱动器,它第一次就可以正常工作。我使用了下面编码的 3 种不同的方法。
失败时我得到的错误号是 286。“文件未保存。请确保您的系统有足够的磁盘空间或网络连接完好”
非常感谢任何建议,
[DllImport("winmm.dll",CharSet=CharSet.Auto)]
private static extern uint mciSendString([MarshalAs(UnmanagedType.LPTStr)] string command,
StringBuilder returnValue,
int returnLength,
IntPtr winHandle);
[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern int mciGetErrorString(uint errorCode, StringBuilder errorText, int errorTextSize);
[DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
private static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string longPath,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
int length);
// Stop recording
private void StopRecording()
{
// Save recorded voice
string shortPath = this.shortPathName();
string formatShortPath = string.Format("save recsound \"{0}\"", shortPath);
uint result = 0;
StringBuilder errorTest = new StringBuilder(256);
// C:\DOCUME~1\Steve\APPLIC~1\Test.wav
// Fails
result = mciSendString(string.Format("{0}", formatShortPath), null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
// command line convention - fails
result = mciSendString("save recsound \"C:\\DOCUME~1\\Steve\\APPLIC~1\\Test.wav\"", null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
// 8.3 short format - fails
result = mciSendString(@"save recsound C:\DOCUME~1\Steve\APPLIC~1\Test.wav", null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
// Save to C drive works everytime.
result = mciSendString(@"save recsound C:\Test.wav", null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
mciSendString("close recsound ", null, 0, IntPtr.Zero);
}
// Get the short path name so that the mciSendString can save the recorded wave file
private string shortPathName()
{
string shortPath = string.Empty;
long length = 0;
StringBuilder buffer = new StringBuilder(256);
// Get the length of the path
length = GetShortPathName(this.saveRecordingPath, buffer, 256);
shortPath = buffer.ToString();
return shortPath;
}