我阅读了一些关于如何使用mciSendString()函数在VB.Net中播放WAV文件的教程。由于某种原因,它成功地录制并将声音保存到文件中(我用声音应用程序收听了文件),但没有播放任何内容并且没有触发错误。
以下是代码:
'Start recording
If OvalShape1.FillColor = Color.DarkRed Then
OvalShape1.FillColor = Color.Red
Try
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("record recsound", "", 0, 0)
Catch ex As Exception
MessageBox.Show(ex.ToString)
OvalShape1.FillColor = Color.DarkRed
End Try
'Stop recording
Else
OvalShape1.FillColor = Color.DarkRed
Try
'generate unique filename and save as tmp file
Dim TempFullFileName As String = IO.Path.GetTempFileName()
'save recording
mciSendString("save recsound " & Chr(34) & TempFullFileName & Chr(34), "", 0, 0)
mciSendString("close recsound", "", 0, 0)
mciSendString("open " & Chr(34) & TempFullFileName & Chr(34) & " Alias recsound", "", 0, 0)
mciSendString("play recsound", Nothing, 0, 0)
mciSendString("close recsound", "", 0, 0)
Catch ex As Exception
MessageBox.Show(ex.ToString)
OvalShape1.FillColor = Color.DarkRed
End Try
End If
最初,我只是保存并播放了文件,接下来我关闭/重新打开了声音文件,但没有任何区别。我错过了什么吗?
谢谢。
编辑:
Try / catch不显示mciSendString返回的错误 - >使用mciGetErrorString()检查返回值
要播放整个声音文件,您必须在关闭之前添加“等待”
这是一些有效的代码:
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
Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer
Private Function GetMCIErrorString(ByVal ErrorCode As Integer) As String
GetMCIErrorString = Space(1024)
mciGetErrorString(ErrorCode, GetMCIErrorString, Len(GetMCIErrorString))
GetMCIErrorString = Trim(GetMCIErrorString)
End Function
Private Sub Form1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
Dim SoundFile As String = "C:\Documents and Settings\joe\Local Settings\Temp\tmp8FED.wav"
SoundFile = ControlChars.Quote & SoundFile & ControlChars.Quote
Dim StringToOpen As String = "open " & DummyFile & " alias recsound"
ErrCode = mciSendString(StringToOpen, "", 0, 0)
If ErrCode <> 0 Then
MessageBox.Show(GetMCIErrorString(ErrCode))
Exit Sub
End If
ErrCode = mciSendString("play recsound wait", Nothing, 0, 0)
If ErrCode <> 0 Then
MessageBox.Show(GetMCIErrorString(ErrCode))
Exit Sub
End If
ErrCode = mciSendString("close recsound", "", 0, 0)
If ErrCode <> 0 Then
MessageBox.Show(GetMCIErrorString(ErrCode))
Exit Sub
End If
End Sub