Advertisement
mr3amo

Class BasicMciPlayer

Nov 14th, 2020 (edited)
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Mp3Player.Play(soundfile, False)
  2. Public Class BasicMciPlayer
  3.     ' لتشغيل ملفات MP3 , WAV , MID , WMA , ASF , AVI ,
  4.    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
  5.     Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer
  6.  
  7.     Public Event MediaOpened(ByVal sender As System.Object, ByVal e As System.EventArgs)
  8.     Public Event MediaClosed(ByVal sender As System.Object, ByVal e As System.EventArgs)
  9.  
  10.     Private Shared ObjectCount As Integer = 0
  11.     Private retVal As Integer = 0
  12.     Private returnData As String = Space(128)
  13.     Private errorString As String = Space(128)
  14.     Private errorSuccess As Boolean
  15.     '
  16.    Sub New()
  17.         ObjectCount = ObjectCount + 1
  18.         mFileAlias = "SND" & Hex(Now.Ticks) & Hex(ObjectCount)
  19.     End Sub
  20.  
  21.     Sub New(ByVal pFileName As String)
  22.         Me.New()
  23.         Me.FileName = pFileName
  24.     End Sub
  25.  
  26.  
  27.     Private mFileAlias As String
  28.     ReadOnly Property FileAlias() As String
  29.         Get
  30.             Return mFileAlias
  31.         End Get
  32.     End Property
  33.  
  34.     Private mFileName As String
  35.     Property FileName() As String
  36.         Get
  37.             Return mFileName
  38.         End Get
  39.         Set(ByVal value As String)
  40.             Me.Stop()
  41.             Me.close()
  42.             mFileName = value
  43.         End Set
  44.     End Property
  45.  
  46.     Private mLength As Long = 0
  47.     Public ReadOnly Property Length() As Long
  48.         Get
  49.             Return mLength
  50.         End Get
  51.     End Property
  52.  
  53.     Public Property Position() As Long
  54.         Get
  55.             retVal = mciSendString("status " & Me.FileAlias & " position", returnData, 128, 0)
  56.             Return Val(returnData)
  57.         End Get
  58.         Set(ByVal value As Long)
  59.             If Me.IsPlaying() Then
  60.                 retVal = mciSendString("play " & Me.FileAlias & " from " & value.ToString, 0, 0, 0)
  61.             Else
  62.                 retVal = mciSendString("seek " & Me.FileAlias & " to " & value.ToString, 0, 0, 0)
  63.             End If
  64.         End Set
  65.     End Property
  66.  
  67.  
  68.     Public ReadOnly Property IsPlaying() As Boolean
  69.         Get
  70.             retVal = mciSendString("status " & Me.FileAlias & " mode", returnData, 128, 0)
  71.             Return returnData.StartsWith("playing")
  72.         End Get
  73.     End Property
  74.  
  75.  
  76.     Private mVolume As Byte = 100
  77.     Public Property volume() As Byte
  78.         Get
  79.             Return mVolume
  80.         End Get
  81.         Set(ByVal value As Byte)
  82.             If value > 100 Then
  83.                 value = 100
  84.             End If
  85.             mVolume = value
  86.  
  87.             Dim vol As Integer = (Me.volume * 10)
  88.             retVal = mciSendString("setaudio " & Me.FileAlias & " volume to " & vol.ToString, 0, 0, 0)
  89.  
  90.         End Set
  91.     End Property
  92.  
  93.     Private mIsOpen As Boolean = False
  94.     Sub open(ByVal sFileName As String)
  95.         Me.FileName = sFileName
  96.         Me.open()
  97.     End Sub
  98.  
  99.     Public ScreenHandel As Int32 = 0
  100.  
  101.     Sub open()
  102.         Me.close()
  103.         If ScreenHandel <= 0 Then
  104.             retVal = mciSendString("open """ & Me.FileName & """ type mpegvideo alias " & Me.FileAlias, 0, 0, 0)
  105.         Else
  106.             retVal = mciSendString("open """ & Me.FileName & """ type mpegvideo alias " & Me.FileAlias & " parent " & ScreenHandel.ToString & " style " & "child" & " ", 0, 0, 0)
  107.         End If
  108.  
  109.         retVal = mciSendString("set " & Me.FileAlias & " time format ms", 0, 0, 0)
  110.  
  111.         'Get the length of the currently opened song in milli-seconds.
  112.        retVal = mciSendString("status " & Me.FileAlias & " length", returnData, 128, 0)
  113.         mLength = Val(returnData)
  114.         mIsOpen = True
  115.         Me.volume = Me.volume
  116.         RaiseEvent MediaOpened(Me, New System.EventArgs)
  117.     End Sub
  118.  
  119.     Sub Play(Optional ByVal repate As Boolean = False)
  120.         If repate = True Then
  121.             Me.Play("repeat")
  122.         Else
  123.             Play("")
  124.         End If
  125.     End Sub
  126.  
  127.  
  128.     Sub Play(ByVal pFileName As String, ByVal pRepate As Boolean)
  129.         Me.FileName = pFileName
  130.         Me.Play(pRepate)
  131.     End Sub
  132.  
  133.     Sub Play(ByVal pCommand As String)
  134.         Dim Cmd As String = "play " & Me.FileAlias & " " & pCommand
  135.         If mIsOpen = False Then
  136.             Me.close()
  137.             Me.open()
  138.         End If
  139.         retVal = mciSendString(Cmd, 0, 0, 0)
  140.     End Sub
  141.  
  142.  
  143.     Sub Pause()
  144.         retVal = mciSendString("pause " & Me.FileAlias, 0, 0, 0)
  145.     End Sub
  146.  
  147.  
  148.     Sub [resume]()
  149.         retVal = mciSendString("resume " & Me.FileAlias, 0, 0, 0)
  150.     End Sub
  151.  
  152.     Sub [Stop]()
  153.         retVal = mciSendString("stop " & Me.FileAlias, 0, 0, 0)
  154.     End Sub
  155.  
  156.  
  157.     Sub close()
  158.         Me.Stop()
  159.         retVal = mciSendString("close " & Me.FileAlias, 0, 0, 0)
  160.         '----------------------------------
  161.        If mIsOpen = True Then
  162.             RaiseEvent MediaClosed(Me, New System.EventArgs)
  163.         End If
  164.         '----------------------------------
  165.        mIsOpen = False
  166.         mLength = 0
  167.         '--------------------------------------
  168.  
  169.     End Sub
  170.  
  171.     Protected Overrides Sub Finalize()
  172.         Me.close()
  173.         MyBase.Finalize()
  174.     End Sub
  175. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement