VS C#2008 SP1
我创建了一个记录和播放音频的小应用程序。但是,我的应用程序需要将wave文件保存到用户计算机上的应用程序数据目录中。
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;
}
答案 0 :(得分:3)
我在C drive
和D drive
中测试它,只要目录路径不包含空格,它就可以工作。 "C:\Test.wav"
和"D:\mp3\test.wav"
都有效,但"D:\mp4 folder\test.wav"
不起作用。
答案 1 :(得分:1)
试试这个:
[ string directoryString =“C:\ DOCUME~1 \ Steve \ APPLIC~1 \”; Directory.SetCurrentDirectory(directoryString); mciSendString(@“save recsound Test.wav”,null,0,IntPtr.Zero); mciSendString(“close recsound”,null,0,IntPtr.Zero); ]