十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
My.Computer.Audio.Play("SoundFile.wav")
创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站制作、网站建设、武安网络推广、小程序开发、武安网络营销、武安企业策划、武安品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供武安建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
SoundFile.wav是你要播放的声音文件的路径
1.用Win32 API PlaySound
PlaySound位于"winmm.dll"中,可以根据输入参数的不同,播放WAV类型的音乐。在VB.NET中调用跟VB6中差不多:
先声明:
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
Const SND_FILENAME As Integer = H20000
Const SND_ALIAS As Integer = H10000
Const SND_SYNC As Integer = H0
具体还有很多常量,不一一列举,下面是播放一个wav文件的例子
Dim mstrfileName As String = "c:\eagle2.wav"
PlaySound(mstrfileName, 0, SND_FILENAME)
注意上面的播放是非同步的,就是说它并不会播放完毕才结束。如果把上面的代码写在一个Button.Click的事件处理程序中,第二次点击的时候它会打断第一次的播放,重新开始。
当然我们也可以调用系统的声音:
PlaySound("SystemStart", 0, SND_ALIAS Or SND_SYNC)
' 以同步的方式调用系统启动时候的声音。
2. 调用媒体播放器控件
VB.NET中并没有提供媒体播放器的.NET组件,没办法我们还是调用以前Com组件Windows Media Player。当然除了声音外,还可以播放视频文件。
首先把媒体播放器控件加到工具栏中,调用还是简单的写一下吧:)
Private Sub playMediaFile(ByVal mediaFileName As String)
With MediaPlayer1
.Stop()
.FileName = "c:\mp3\爱不爱我.mp3"
.Play()
End With
End Sub
A simple Sound Recorder~
1. Add Three buttons (button1, button2, button3) and a label (label1) to the form.
2. Set the Text of Button1 to Start, Button2 to Stop, and Button3 to Play.
3. Add this code:
Public Class Form1
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Button2.Enabled = True
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("record recsound", "", 0, 0)
Label1.Text = "Recording..."
Label1.Visible = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = True
mciSendString("save recsound c:\recsound.wav", "", 0, 0)
mciSendString("close recsound", "", 0, 0)
MsgBox("File Created: C:\recsound.wav")
Label1.Text = "Stopped..."
Label1.Visible = False
My.Computer.Audio.Stop()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Label1.Text = "Playing..."
Label1.Visible = True
My.Computer.Audio.Play("c:\recsound.wav", AudioPlayMode.Background)
End Sub
End Class
安装了sdk后应该可以在控件箱单击鼠标右键,然后选择部件,在弹出的列表中应该可以找到microsoft tts engine或之类的东西,选择后就可以吧tts engine作为控件放到窗体上。 调用貌似是用.speak(String)实现的。 由于我是很久以前用的,所以也记得不是很清楚了,你可以找找看。