vb playsound 具体函数如何使用?求详细代码解析
发布网友
发布时间:2022-04-25 12:24
我来回答
共2个回答
热心网友
时间:2024-03-29 18:06
方法很简单。不过首先要声明函数和基本常数
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hMole As Long, ByVal dwFlags As Long) As Long
Private Const SND_ASYNC = &H1 ' play asynchronously
Private Const SND_FILENAME = &H20000 ' name is a file name
Private Const SND_LOOP = &H8 ' loop the sound until next sndPlaySound
连续播放的语句如下:
PlaySound "The Microsoft Sound.wav", 0, SND_FILENAME Or SND_ASYNC Or SND_LOOP
SND_FILENAME表示第一个参数为文件路径,SND_ASYNC表示异步播放,即马上返回(而不是等播放完毕后才返回),SND_LOOP则表示循环播放。
要停止则用:
PlaySound 0,0,0
因为前面已经定义了播放方式为异步,所以这里只需将所有参数置0即可。
再补充一点:用PlaySound虽然简单,但也有其局限性,如果文件稍大,就不能采用这种方式了。此时需要采用DirectSound,当然其调用方式也复杂一些。
热心网友
时间:2024-03-29 18:07
Option Explicit
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrRetumString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Dim soundfile, flag
Function playalarmsound(mp3filename, flag)
If Dir(mp3filename) <> "" Then
If flag = 1 Then
mciSendString "play " & mp3filename & " repeat", vbNullString, 0, 0
Else
mciSendString "close " & mp3filename, 0&, 0, 0
End If
End If
End Function
Private Sub Command1_Click()
soundfile = "e:\歌曲\mp3\下雨天..mp3"
flag = 1 '播放
playalarmsound soundfile, flag
End Sub
Private Sub Form_Unload(Cancel As Integer)
playalarmsound soundfile, 0 '停止
End Sub