Private sAlias As String ' Used internally to give an alias name to
' the multimedia resource
Private sFilename As String ' Holds the filename internally
Private nLength As Single ' Holds the length of the filename
' internally
Private nPosition As Single ' Holds the current position internally
Private sStatus As String ' Holds the current status as a string
Private bWait As Boolean ' Determines if VB should wait until play
' is complete before returning.
'note that this is all one code line:
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
Public Sub mmOpen(ByVal sTheFile As String)
' Declare a variable to hold the value returned by mciSendString
Dim nReturn As Long
' Declare a string variable to hold the file type
Dim sType As String
' Opens the specified multimedia file, and closes any
' other that may be open
If sAlias <> "" Then
mmClose
End If
' Determine the type of file from the file extension
Select Case UCase$(Right$(sTheFile, 3))
Case "WAV"
sType = "Waveaudio"
Case "AVI"
sType = "AviVideo"
Case "MID"
sType = "Sequencer"
Case "MP3"
sType = "MPEGVideo"
'sType = "MPEGVideo" 时 可以播放的不仅是Mp3音乐了.
'我只是添加了 Case "MP3" sType = "MPEGVideo",不过功能是大大加强哦.
Case Else
' If the file extension is not known then exit the subroutine
Exit Sub
End Select
sAlias = Right$(sTheFile, 3) & Minute(Now)
' At this point there is no file open, and we have determined the
' file type. Now would be a good time to open the new file.
' Note: if the name contains a space we have to enclose it in quotes
If InStr(sTheFile, " ") Then sTheFile = Chr(34) & sTheFile & Chr(34)
nReturn = mciSendString("Open " & sTheFile & " ALIAS " & sAlias _
& " TYPE " & sType & " wait", "", 0, 0)
End Sub
Public Sub mmClose()
' Closes the currently opened multimedia file
' Declare a variable to hold the return value from the mciSendString
' command
Dim nReturn As Long
' If there is no file currently open then exit the subroutine
If sAlias = "" Then Exit Sub
Public Sub mmPlay()
' Plays the currently open file, from the current position
' Declare a variable to hold the return value from the mciSendString
' command
Dim nReturn As Long
' If there is no file currently open, then exit the routine
If sAlias = "" Then Exit Sub
' Now play the file
If bWait Then
nReturn = mciSendString("Play " & sAlias & " wait", "", 0, 0)
Else
nReturn = mciSendString("Play " & sAlias, "", 0, 0)
End If
End Sub
Public Sub mmStop()
' Stop using a file totally, be it playing or whatever
' Declare a variable to hold the return value from mciSendString
Dim nReturn As Long
' If there is no file currently open then exit the subroutine
If sAlias = "" Then Exit Sub
Property Get Filename() As String
' Routine to return a value when the programmer asks the
' object for the value of its Filename property
Filename = sFilename
End Property
Property Let Filename(ByVal sTheFile As String)
' Routine to set the value of the filename property, should the programmer
' wish to do so. This implies that the programmer actually wants to open
' a file as well so control is passed to the mmOpen routine
mmOpen sTheFile
End Property
Property Get Wait() As Boolean
' Routine to return the value of the object's wait property.
Wait = bWait
End Property
Property Let Wait(bWaitValue As Boolean)
' Routine to set the value of the object's wait property
bWait = bWaitValue
End Property
Property Get Length() As Single
' Routine to return the length of the currently opened multimedia file
' Declare a variable to hold the return value from the mciSendString
Dim nReturn As Long, nLength As Integer
' Declare a string to hold the returned length from the mci Status call
Dim sLength As String * 255
' If there is no file open then return 0
If sAlias = "" Then
Length = 0
Exit Property
End If
'窗体中的代码
VERSION 5.00
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.3#0"; "comctl32.ocx"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmMain
Caption = "Mp3Player"
ClientHeight = 4275
ClientLeft = 60
ClientTop = 450
ClientWidth = 8205
LinkTopic = "Form1"
LockControls = -1 'True
ScaleHeight = 4275
ScaleWidth = 8205
StartUpPosition = 2 'CenterScreen
Begin VB.TextBox Text4
Height = 375
Left = 5880
TabIndex = 10
Text = "1000"
Top = 3240
Width = 855
End
Begin VB.TextBox Text3
Height = 375
Left = 360
TabIndex = 9
Top = 720
Width = 6975
End
Begin VB.TextBox Text2
Height = 375
Left = 360
TabIndex = 8
Top = 360
Width = 6975
End
Begin VB.TextBox Text1
Height = 375
Left = 360
TabIndex = 7
Top = 0
Width = 6975
End
Begin VB.CommandButton Command7
Caption = "停止"
Height = 615
Left = 4920
TabIndex = 6
Top = 1920
Width = 855
End
Begin VB.CommandButton Command6
Caption = "快近"
Height = 615
Left = 4920
TabIndex = 5
Top = 3480
Width = 855
End
Begin VB.CommandButton Command5
Caption = "快退"
Height = 615
Left = 4920
TabIndex = 4
Top = 2760
Width = 855
End
Begin VB.CommandButton Command4
Caption = "暂停"
Height = 615
Left = 3870
TabIndex = 3
Top = 1920
Width = 855
End
Begin VB.CommandButton Command3
Caption = "播放"
Height = 615
Left = 2895
TabIndex = 2
Top = 1920
Width = 855
End
Begin MSComDlg.CommonDialog CommonDialog1
Left = 1320
Top = 3360
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
Begin VB.CommandButton Command1
Caption = "打开和播放一个媒体文件"
Height = 615
Left = 480
TabIndex = 1
Top = 1920
Width = 2172
End
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 500
Left = 2040
Top = 3360
End
Begin ComctlLib.ProgressBar ProgressBar1
Height = 255
Left = 360
TabIndex = 0
Top = 1440
Width = 4815
_ExtentX = 8493
_ExtentY = 450
_Version = 327682
Appearance = 1
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Multimedia As New Mmedia
Private Sub Command1_Click()
With CommonDialog1
.Filter = "Mp3 (*.mp3)|*.mp3|WaveAudio (*.wav)|*.wav|Video files (*.avi)|*.avi"
.FilterIndex = 0
.ShowOpen
End With
If CommonDialog1.Filename <> "" Then
Multimedia.Wait = False
Multimedia.mmOpen CommonDialog1.Filename
ProgressBar1.Value = 0
ProgressBar1.Max = Multimedia.Length
End If
End Sub
Private Sub Command3_Click()
Timer1.Enabled = True
Multimedia.mmPlay
End Sub
Private Sub Command4_Click()
Multimedia.mmPause
End Sub
Private Sub Command5_Click()
If Multimedia.Status <> "playing" Then Exit Sub