خالد مسعد . |
09-02-2010 07:04 PM |
كود مشغل mp3 , wav , avi , asf , mdi ,wmv
لتشغيل ملفات الصوت بشكل بسيط ، يمكن كتابة هذين السطرين
كود:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
mciSendString("open C:\Hadeia.mp3 type mpegvideo alias thissong", 0, 0, 0)
mciSendString("play thissong", 0, 0, 0)
End Sub
لتشغيل ملفات الصوت بشكل اكثر تحكم اضف هذا الكلاس BasicMciPlayer الى مشروعك
واكتب هذا الكود لشغيل الصوت
كود:
Static Mp3Player As New BasicMciPlayer
Mp3Player.Play("C:\Hadeia.mp3", False)
كلاس BasicMciPlayer
كود:
Public Class BasicMciPlayer
' لتشغيل ملفات MP3 , WAV , MID , WMA , ASF , AVI ,
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer
Public Event MediaOpened(ByVal sender As System.Object, ByVal e As System.EventArgs)
Public Event MediaClosed(ByVal sender As System.Object, ByVal e As System.EventArgs)
Private Shared ObjectCount As Integer = 0
Private retVal As Integer = 0
Private returnData As String = Space(128)
Private errorString As String = Space(128)
Private errorSuccess As Boolean
'
Sub New()
ObjectCount = ObjectCount + 1
mFileAlias = "SND" & Hex(Now.Ticks) & Hex(ObjectCount)
End Sub
Sub New(ByVal pFileName As String)
Me.New()
Me.FileName = pFileName
End Sub
Private mFileAlias As String
ReadOnly Property FileAlias() As String
Get
Return mFileAlias
End Get
End Property
Private mFileName As String
Property FileName() As String
Get
Return mFileName
End Get
Set(ByVal value As String)
Me.Stop()
Me.close()
mFileName = value
End Set
End Property
Private mLength As Long = 0
Public ReadOnly Property Length() As Long
Get
Return mLength
End Get
End Property
Public Property Position() As Long
Get
retVal = mciSendString("status " & Me.FileAlias & " position", returnData, 128, 0)
Return Val(returnData)
End Get
Set(ByVal value As Long)
If Me.IsPlaying() Then
retVal = mciSendString("play " & Me.FileAlias & " from " & value.ToString, 0, 0, 0)
Else
retVal = mciSendString("seek " & Me.FileAlias & " to " & value.ToString, 0, 0, 0)
End If
End Set
End Property
Public ReadOnly Property IsPlaying() As Boolean
Get
retVal = mciSendString("status " & Me.FileAlias & " mode", returnData, 128, 0)
Return returnData.StartsWith("playing")
End Get
End Property
Private mVolume As Byte = 100
Public Property volume() As Byte
Get
Return mVolume
End Get
Set(ByVal value As Byte)
If value > 100 Then
value = 100
End If
mVolume = value
Dim vol As Integer = (Me.volume * 10)
retVal = mciSendString("setaudio " & Me.FileAlias & " volume to " & vol.ToString, 0, 0, 0)
End Set
End Property
Private mIsOpen As Boolean = False
Sub open(ByVal sFileName As String)
Me.FileName = sFileName
Me.open()
End Sub
Public ScreenHandel As Int32 = 0
Sub open()
Me.close()
If ScreenHandel <= 0 Then
retVal = mciSendString("open """ & Me.FileName & """ type mpegvideo alias " & Me.FileAlias, 0, 0, 0)
Else
retVal = mciSendString("open """ & Me.FileName & """ type mpegvideo alias " & Me.FileAlias & " parent " & ScreenHandel.ToString & " style " & "child" & " ", 0, 0, 0)
End If
retVal = mciSendString("set " & Me.FileAlias & " time format ms", 0, 0, 0)
'Get the length of the currently opened song in milli-seconds.
retVal = mciSendString("status " & Me.FileAlias & " length", returnData, 128, 0)
mLength = Val(returnData)
mIsOpen = True
Me.volume = Me.volume
RaiseEvent MediaOpened(Me, New System.EventArgs)
End Sub
Sub Play(Optional ByVal repate As Boolean = False)
If repate = True Then
Me.Play("repeat")
Else
Play("")
End If
End Sub
Sub Play(ByVal pFileName As String, ByVal pRepate As Boolean)
Me.FileName = pFileName
Me.Play(pRepate)
End Sub
Sub Play(ByVal pCommand As String)
Dim Cmd As String = "play " & Me.FileAlias & " " & pCommand
If mIsOpen = False Then
Me.close()
Me.open()
End If
retVal = mciSendString(Cmd, 0, 0, 0)
End Sub
Sub Pause()
retVal = mciSendString("pause " & Me.FileAlias, 0, 0, 0)
End Sub
Sub [resume]()
retVal = mciSendString("resume " & Me.FileAlias, 0, 0, 0)
End Sub
Sub [Stop]()
retVal = mciSendString("stop " & Me.FileAlias, 0, 0, 0)
End Sub
Sub close()
Me.Stop()
retVal = mciSendString("close " & Me.FileAlias, 0, 0, 0)
'----------------------------------
If mIsOpen = True Then
RaiseEvent MediaClosed(Me, New System.EventArgs)
End If
'----------------------------------
mIsOpen = False
mLength = 0
'--------------------------------------
End Sub
Protected Overrides Sub Finalize()
Me.close()
MyBase.Finalize()
End Sub
End Class
|