2.
以下是王国荣、郑郁霖先生的文章
利用多媒体的 mciSendString API 函数, 再搭配 mci(multimedia control interface) 指令, 即
可播放 .wav 声音档, 细节如下:
1. API 的宣告:
Declare Function mciSendString Lib "Winmm.dll" Alias "mciSendStringA"_
(ByVal Command As String,_
ByVal ReturnString As String,_
ByVal ReturnLength As Long,_
ByVal Callback As Long) As Long
Command : MCI 的命令字串
open = 开启设备:设备名 [type 设备型式][alias 别名]
close = 关闭设备:设备名
play = 播放档案:设备名 [from 起点][to 终点]
pause = 暂停档案
resume = 取消暂停档案
seek = 直接移到:设备名 [to 位置 | to start | to end]
stop = 停止拨放:设备名
1.应该可以用的,你按照这个方法试一下呢
'This project needs a Common Dialog box, named 'CDBox'
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Dim Alias As String
Private Sub Form_Load()
Const PlayTime = 10
'Set the common dialog box' title
CDBox.DialogTitle = "Choose your midi-file"
'Set the filter
CDBox.Filter = "Midi-files (*.mid)|*.mid"
'Show the 'Open File'-dialog
CDBox.ShowOpen
'Extract an alias from the file
Alias = Left$(CDBox.FileTitle, Len(CDBox.FileTitle) - 4)
'play midi
R% = mciSendString("OPEN " + CDBox.filename + " TYPE SEQUENCER ALIAS " + Alias, 0&, 0, 0)
R% = mciSendString("PLAY " + Alias + " FROM 0", 0&, 0, 0)
R% = mciSendString("CLOSE ANIMATION", 0&, 0, 0)
'play midi for 10 secs
t = Timer
Do: DoEvents: Loop Until Timer > t + PlayTime
'stop midi and close it
R% = mciSendString("OPEN " + CDBox.filename + " TYPE SEQUENCER ALIAS " + Alias, 0&, 0, 0)
R% = mciSendString&("STOP " + Alias, 0&, 0, 0)
R% = mciSendString&("CLOSE ANIMATION", 0&, 0, 0)
End Sub
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySound
A" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
以及如下的标识常量:
Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_ALIAS = &H10000
Public Const SND_FILENAME = &H20000
Public Const SND_RESOURCE = &H40004
Public Const SND_ALIAS_ID = &H110000
Public Const SND_ALIAS_START = 0
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public Const SND_VALID = &H1F
Public Const SND_NOWAIT = &H2000
Public Const SND_VALIDFLAGS = &H17201F
Public Const SND_RESERVED = &HFF000000
Public Const SND_TYPE_MASK = &H170007
Dim ReturnValue As Long
ReturnSoundValue = mciExecute("play C:\WIN95\MEDIA\CANYON.MID from 10 to
100")
mciExecute函数也可以用来对WAV文件进行播放,方法同播放MIDI一样。
样例程序
1.在Visual Basic中开始一个新的项目,采用缺省的方法建立Form1。
2.将如下的声明语句添加到Form1的通常声明部分中(注意每个声明语句需要被书写
在一行内):
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySound
A" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Sub Command1_Click()
Dim ReturnValue As Long
Text1.Text = "使用sndPlaySound函数播放TADA.WAV文件。"
ReturnValue = sndPlaySound("C:\WIN95\MEDIA\TADA.WAV", SND_SYNC)
End Sub
7.将如下的代码添加到Command2的单击事件中:
Private Sub Command2_Click()
Dim ReturnValue As Long
Text1.Text = "使用mciExecute函数播放Canyon.mid文件。"
ReturnSoundValue = mciExecute
("play C:\WIN95\MEDIA\CANYON.MID from 10 to 100")
End Sub
Private Sub Command_Open_Click()
Me.CommonDialog1.ShowOpen
m_Filename = Me.CommonDialog1.FileName
If m_Filename <> "" Then
Me.MMControl1.FileName = m_Filename
Me.Caption = m_Filename
Me.MMControl1.Command = "Open"
End If
End Sub
Private Sub Command_Play_Click()
Me.MMControl1.Command = "Play"
End Sub
Private Sub Form_Load()
m_Filename = ""
Me.MMControl1.AutoEnable = True
Me.MMControl1.hWndDisplay = Me.hWnd
End Sub
Private Sub MMControl1_StatusUpdate()
Me.HScroll1.Max = Me.MMControl1.Length
Me.HScroll1.Min = 0
Me.HScroll1.Value = Me.MMControl1.Position
End Sub