无外部控件制作多媒体播放器(一)

80酷酷网    80kuku.com

  控件|媒体
利用MCI指令制作播放器,简单实用,很适合于做为自己软件的一个附带功能或背景音乐,正是基于这点需求,我准备分几个部分来写:

1、MCI指令的简单使用;
2、媒体播放的进度控制与音量调节;
3、音乐信息的读取,包括MP3(ID3V1 & ID3V2)与ASF(WMA & WMV)等;
4、音乐列表的建立与保存(M3U格式)

本来主要是想写播放音乐的,举个播放视频的例子,没什么别的意思,只是感觉播放音乐实在是简单,没什么可写,同时也是为了说明,MCI放视频也是可以的。

Private Const WS_CHILD = &H40000000
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 Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Function ShortName(LName As String) As String
    '取得短文件名
    Dim s As String, i As Long
    i = 512
    s = Space$(i)
    GetShortPathName LName, s, i
    ShortName = Left$(s, InStr(1, s, vbNullChar) - 1)
End Function

Private Function PlayMCI(Cmd As String, Optional ReturnStr As String) As Long
    '播放MCI
    Dim s As String
    s = Space$(256)
    PlayMCI = mciSendString(Cmd, s, 256, 0)
    ReturnStr = Left$(s, InStr(1, s, vbNullChar) - 1)
End Function

Private Function ShowVideo(strFileName As String, hwd As Long, x As Long, y As Long, w As Long, h As Long) As Long
    Dim i As Long, s As String
    If Dir(strFileName, vbHidden Or vbReadOnly Or vbSystem) = vbNullString Or strFileName = vbNullString Then Exit Function
    i = PlayMCI("open """ & ShortName(strFileName) & """ alias Song parent " & hwd & " style " & WS_CHILD & " WAIT")
    If i <> 0 Then Exit Function
    i = PlayMCI("STATUS Song WINDOW HANDLE WAIT", s)
    If i <> 0 Then GoTo fail
    i = Val(s)
    If i = 0 Then GoTo fail
    SetWindowPos i, 0, x, y, w, h, 0
    PlayMCI "play Song"
    ShowVideo = i   '若成功返回视频窗口的句柄
    Exit Function
fail:
    PlayMCI "close Song"
End Function

Private Sub cmdPlay_Click()
    i=ShowVideo("h:\1.wmv", Me.hWnd, 0, 0, 100, 100)
   '返回的这个句柄,很有用的,可用于移动窗口位置,或SubClass它,加上弹出菜单,响应鼠标动作等
    If i <> 0 Then
        cmdPlay.Enabled = False
        cmdStop.Enabled = True
    End If
End Sub
Private Sub cmdStop_Click()
    PlayMCI "close Song"
    cmdPlay.Enabled = True
    cmdStop.Enabled = False
End Sub

Private Sub Form_Load()
    Me.ScaleMode = 3
    cmdPlay.Enabled = True
    cmdStop.Enabled = False
    cmdPlay.Caption = "播放"
    cmdStop.Caption = "停止"
End Sub
Private Sub Form_Unload(Cancel As Integer)
    PlayMCI "close Song"
End Sub


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: