Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
<DllImport("winmm.dll", CharSet:=CharSet.Ansi, EntryPoint:="mciSendString")>
Shared Function mciSendString(ByVal lpstrCommand As String, ByRef lpstrReturnString As StringBuilder, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
End Function
<DllImport("winmm.dll", CharSet:=CharSet.Ansi, EntryPoint:="mciGetErrorString")>
Shared Function mciGetErrorString(ByVal dwError As Integer, ByRef lpstrBuffer As StringBuilder, ByVal uLength As Integer) As Integer
End Function
Dim ReturnString As New StringBuilder(256)
Dim RetValue As Integer
Dim errorstring As New StringBuilder(1024)
Private Sub Form_Unload(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
mciSendString("close all", ReturnString, 256, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'成不同的文件名,例如基于当前时间
Dim fileName As String = "Sound_" & DateTime.Now.ToString("yyyyMMddHHmmss") & ".Wav"
RetValue = mciSendString("stop sounds1", ReturnString, 256, 0)
RetValue = mciSendString("save sounds1 " & fileName, ReturnString, 256, 0)
If RetValue = 0 Then
Else
mciGetErrorString(RetValue, errorstring, 1024)
System.Windows.Forms.MessageBox.Show(errorstring.ToString)
End If
RetValue = mciSendString("close sounds1", ReturnString, 256, 0)
Button2.Enabled = False
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim fileName As String = "Sound_" & DateTime.Now.ToString("yyyyMMddHHmmss") & ".Wav"
If IO.File.Exists(Application.StartupPath & "\" & fileName) Then
RetValue = mciSendString("open " & fileName & " alias sounds1", ReturnString, 256, 0)
mciSendString("play sounds1", ReturnString, 256, 0)
Else
MessageBox.Show("没有声音文件 " & fileName, "提示", MessageBoxButtons.OK)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RetValue = mciSendString("open new type waveaudio alias sounds1", ReturnString, 256, 0)
If RetValue = 0 Then
'开始录音...
RetValue = mciSendString("record sounds1", ReturnString, 256, 0)
Button2.Enabled = True
Else
mciGetErrorString(RetValue, errorstring, 1024)
System.Windows.Forms.MessageBox.Show(errorstring.ToString)
End If
End Sub
End Class