我正在用Visual Basic 2008编写一个程序,我需要有不同类型的声音和不同的音量。因此,My.Computer.Audio.Play不是有效的选项。
我决定改用mciSendString,并找到了以下代码
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
mciSendString("close myWAV", Nothing, 0, 0)
Dim fileName1 As String =
mciSendString("open " & fileName1 & " type mpegvideo alias myWAV", Nothing, 0, 0)
mciSendString("play myWAV", Nothing, 0, 0)
'min Volume is 1, max Volume is 1000
Dim Volume As Integer = (SFXVolume * 100)
mciSendString("setaudio myWAV volume to " & Volume, Nothing, 0, 0)现在,我测试了这段代码,当filename1 =“C://校正.wav”时,它工作得很好。
但是,当我使用
filename1 = My.Application.Info.DirectoryPath & "\Correct.wav“
我没有得到任何声音播放。
谁能帮我纠正我的代码,以便这是工作。提前谢谢你。
发布于 2013-05-20 21:01:27
如果您的DirectoryPath包含空格,那么mciSendString将无法准确识别命令,您需要用引号将路径括起来:
mciSendString(
String.Format("open ""{0}"" type mpegvideo alias myWAV", fileName1), Nothing, 0, 0)按照Hans的建议,一定要检查返回的状态。
此外,由于您不知道DirectoryPath是否有尾随反斜杠,因此从目录和名称生成完整路径的准确方法是:
fileName1 = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Correct.wav")发布于 2016-10-09 21:32:14
在打开要播放的文件之前,先使用Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long,然后使用SetCurrentDirectory filepath。这对我来说很有效。
发布于 2014-01-15 01:14:58
您需要使用DLL调用GetShortPathName来将文件路径传递给WINMM.DLL。lpszLongPath是您的完整路径字符串,而短路径名将传递给lpszShortPath。cchbuffer实际上应该设置为200左右,尽管在大多数情况下,返回的字符串会短得多。您应该使用VB填充字符串。
私有声明函数GetShortPathName Lib "kernel32“别名"GetShortPathNameA”(ByVal lpszLongPath为String,ByVal lpszShortPath为String,ByVal cchBuffer为Long)为Long
我刚刚在一个批处理的midi文件读取程序中使用了mciSendString调用,打开3642个midi文件并返回版权、标题和播放时长字符串,实际上相当快!
向David R Leach致以最好的问候
https://stackoverflow.com/questions/16649299
复制相似问题