(SOURCE : http://merry05.blog.hexun.com.tw/26987503_d.html )
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
Private Sub Form_Load()
Dim musicName As String
musicName = "(全路徑音樂文件名)" '註意:路徑和文件名決不能有空格
mciSendString "open " & musicName & " alias mymusic", 0&, 0, 0
End Sub
Private Sub Command1_Click() '開始播放
mciSendString "play mymusic", "", 0, 0
End Sub
Private Sub Command2_Click() '暫停
mciSendString "pause mymusic", "", 0, 0
End Sub
Private Sub Command3_Click() '繼續
mciSendString "resume mymusic", 0&, 0, 0
End Sub
Private Sub Command4_Click() '停止
mciSendString "stop mymusic", 0&, 0, 0
End Sub
Private Sub Command5_Click() '關閉
mciSendString "close mymusic", 0&, 0, 0
End Sub
----------------------------------
(winmm.dll)API函數mciSendString的詳細資料
(SOURCE : https://sites.google.com/site/syanjiushi/cheng-shi-yu-yan-pai-ming/mci-xiang-xi-zi-liao-vb-ban)
Option Explicit
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
mciSendString是用來播放多媒體文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,下面介紹
它的使用方法:
該函數有四個參數:
第一個參數:lpstrCommand 要發送的命令字符串。字符串結構是:[命令][設備別名][命令參數].
第二個參數:lpstrReturnString 返回信息的緩衝區,為一指定了大小的字符串變量.
第三個參數:uReturnLength 緩衝區的大小,就是字符變量的長度.
第四個參數:hwndCallback 回調方式,一般設為零
返回值:函數執行成功返回零,否則返回錯誤代碼
一、常用命令
1.打開(Open),格式:Open 設備名[type 設備型式][alias 別名]
Dim mName as string
mName = "f:\\mpeg\\mpeg1.avi"
mciSendString "open mName type MPEGVideo Alias movie parent %u Style %u notify",0&, 0, 0
其中:
open 操作命令
mName 全路徑文件名
type MPEGVideo 是指打開MPEG,AVI等類型,如果不加這一句,就是打開WAV,MP3等
Alias movie 定義了該操作的別名為movie,後續操作只要指明別名即可
parent %u 源
Style %u 樣式
notify 通知
2.播放(Play),格式:Play 設備名[from 起點][to 終點]
mciSendString "play movie", 0&, 0, 0
mciSendString "play movie fullscreen", 0&, 0, 0 '全屏播放
3.暫停(Pause):
mciSendString "pause movie", 0&, 0, 0
4.繼續(Resume):
mciSendString "resume movie", 0&, 0, 0
5.停止(Stop):
mciSendString "stop movie", 0&, 0, 0
6.關閉(Colse):
mciSendString "close movie", 0&, 0, 0
7.前進到下一個位置:
mciSendString "step movie", 0&, 0, 0
8.後退到上一個位置:
mciSendString "step movie reverse", 0&, 0, 0
9.前進或後退N 個位置(其中N<0 即表示後退)
mciSendString "step movie by " & str(N), 0&, 0, 0
10.獲取當前播放位置:
Dim ST As String*64
mciSendString "status movie position", st, len(st), 0
11. 獲取媒體的總長度:
mciSendString "status movie length", st, len(st), 0
l=val(st) 'L就是所播放文件的長度
12.獲取播放信息:
Dim ST As String*64
mciSendString "status movie mode", ST, Len(ST), 0
If Left(ST, 7) = "stopped" Then (處理代碼) '播放完畢
13.循環播放:
mciSendString "play movie repeat", 0&, 0, 0
二、控制聲音大小:
Dim V As Long
mciSendString "status movie volume", V, 0, 0 'V是獲取的音量大小值。
V = 50
mciSendString "setaudio movie volume to " & V, &0, 0, 0 'V是設置的音量值
三、設置播放位置.(需事先設定時間格式),格式:Seek 設備名[to 位置| to start | to end]
Dim P1 as Long, P2 as Long
P1 = 100: P2 = 3000
mciSendString "seek movie to ", P1, 0, 0 'P1是當前起始位置,單位:毫秒
mciSendString "seek movie to start", 0&, 0, 0 '定位到開頭位置
mciSendString "play movie", 0&, 0, 0 '定位後再播放
或者:
mciSendString "play movie FROM P1 to P2",0&, 0, 0 'P1是起始位置,P2是停止位置。單位:毫秒
mciSendString "seek movie to end", 0&, 0, 0 '定位到最後位置
四、在指定控件上播放視頻:
mciSendString "open AVI 文件名parent hWnd style child", 0&, 0, 0
其中,hWnd 是控件的句柄
執行上述命令之後,影片會被放置在控件的左上角,且影片的大小不受控件大小的影響,如果想要改變
影片播放的位置及大小,可以在執行play 指令前先執行put 指令,格式如下:
mcisendString "put AVI 文件名window at XY [Width Height]", 0&, 0, 0
其中:X、Y為影片左上角坐標,Width、Height為影片的寬高度
五、如果播放視頻還可控制亮度
Dim B As Long
mciSendString "status movie brightness", B, 0, 0 'B是獲取的亮度值。
B = 50
mciSendString "setvideo movie brightness to " & B, &0, 0, 0 'B是設置的亮度值
六、錄音設置:
錄音前,用以下語句初始化
1.設為8位:
mciSendString "set wave bitpersample 8", "", 0, 0
2.設為11025Hz
mciSendString "set wave samplespersec 11025", "", 0, 0
3.設為立體聲:
mciSendString "set wave channels 2", "", 0, 0
4.實現PCM格式(不一定正確):
MCISENDSTRING "set wave format tag pcm","", 0, 0
5.開始錄音:
mciSendString "close movie",0&,0,0
mciSendString "open new type WAVEAudio alias movie",0&,0,0
mciSendString "record movie",0&,0,0
6.保存錄音到c:\123.wav
mciSendString "stop movie",0&,0,0
mciSendString "save movie C:\\123.wav",0&,0,0
mciSendString "close movie",0&,0,0
七、開關光驅:
mciSendString "set cdaudio door open", "", 0, 0 '打開
mciSendString "set cdaudio door close", "", 0, 0 '關閉
八、其它
1.設置設備的各種狀態(Set)
Set alias_name[audio all off][audio all on][time format ms]:
Set命令用來設置設備的各種狀態.如:靜音,有聲音,時間格式為毫秒等.
2.取得設備的狀態(Status)
Status alias_name[length][mode][position]:
Status命令用來取得設備的狀態.如:該媒體文件的長度,該媒體文件所處狀態,該媒體文件的當前位置等. 的長度,該媒體文件所處狀態,該
媒體文件的當前位置等.
沒有留言:
張貼留言