vb.net mciSendString/winmm.dll - 播放音频文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16649299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:39:32  来源:igfitidea点击:

mciSendString/winmm.dll - Playing an Audio file

vb.netvisual-studio-2008audio

提问by user2396186

I am working on a program in Visual Basic 2008, I am required to have different types of sounds with varying volumes. Hence My.Computer.Audio.Play is not a valid option.

我正在使用 Visual Basic 2008 开发一个程序,我需要具有不同音量的不同类型的声音。因此 My.Computer.Audio.Play 不是一个有效的选项。

I decided to use mciSendString instead and found the following code

我决定改用 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)

Now this code I have tested and is working perfectly when filename1 = "C://Correct.wav"

现在这段代码我已经测试过并且在 filename1 = "C://Correct.wav" 时运行良好

However when I use

但是当我使用

filename1 = My.Application.Info.DirectoryPath & "\Correct.wav"

filename1 = My.Application.Info.DirectoryPath & "\Correct.wav"

I get no sound play whatsoever.

我没有任何声音播放。

Could anyone please help me correct my code so that this works. Thank you in advance.

任何人都可以帮我更正我的代码,以便它可以工作。先感谢您。

回答by Roman R.

If your DirectoryPathhas spaces then mciSendStringwon't be able to recognize the command accurately, you need to surround the path with quotes:

如果您DirectoryPath有空格,mciSendString则将无法准确识别命令,您需要用引号将路径括起来:

mciSendString(
    String.Format("open ""{0}"" type mpegvideo alias myWAV", fileName1), Nothing, 0, 0)

Be sure to check returned status as well, as Hans suggests.

正如 Hans 建议的那样,请务必检查退回的状态。

Also, since you don't know whether DirectoryPathhas a trailing backslash or not, the accurate way to produce full path from directory and name is:

此外,由于您不知道是否DirectoryPath有尾部反斜杠,从目录和名称生成完整路径的准确方法是:

fileName1 = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Correct.wav")

回答by Krzysiek C.

Use Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Longthen SetCurrentDirectory filepathbefore opening file for play. That is working for me.

在打开文件播放之前使用 Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Longthen SetCurrentDirectory filepath。这对我有用。

回答by David R Leach

You need to use the DLL Call GetShortPathName in order to pass file paths to WINMM.DLL. lpszLongPath is your full path string, and the short pathname will be passed to lpszShortPath. cchbuffer should really be set to 200 or so, though in most cases, the returned string will be much shorter. You should use a VB padded string.

您需要使用 DLL Call GetShortPathName 将文件路径传递给 WINMM.DLL。lpszLongPath 是您的完整路径字符串,短路径名将传递给 lpszShortPath。cchbuffer 应该真正设置为 200 左右,尽管在大多数情况下,返回的字符串会短得多。您应该使用 VB 填充字符串。

Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

私有声明函数 GetShortPathName Lib "kernel32" 别名 "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

I have just used the mciSendString calls in a batch midi-file reading programme, opening 3642 midi files and returning copyright, title and play duration strings actually quite quickly!

我刚刚在批处理 MIDI 文件读取程序中使用了 mciSendString 调用,打开 3642 个 MIDI 文件并返回版权、标题和播放持续时间字符串实际上非常快!

Best Regards David R Leach

最好的问候 David R Leach