Trik mainkan musik dengan Visual Basic
Assalamualaikum, wr, wb..
Sebenarnya ini merupakan Trik sederhana pada Visual Basic, tapi kemampuannya cukup lumayan, karena program ini bisa memainkan file sound berformat wave .WAV, file midi .MID, file video .AVI, Cukup hebat bukan ?
OK langsung bahas ya, dah gak sabar ni kayaknya...
Simak Langkah-langkah membuat MIDI Player Visual Basic
- Buka program Visual Basic anda dan mulai dengan form baru
- Masukan CommandButton1
- Name = Command1
- Caption = Load dan mainkan File
- Karna kita akan memainkan file sound, berarti kita harus menentukan atau memilih filenya yang akan dimainkan. Karna itu kita pakai bantuan dengan menambahkan objek DialogBox
- Name = CommondDialog1
- Masukan listing kode dibawah ini pada Command1_click
Private Sub Command1_Click()
Dim Multimedia As New Mmedia
With CommonDialog1
.Filter = "WaveAudio (*.wav)|*.wav|Midi (*.mid)|*.mid|Video files (*.avi)|*.avi"
.FilterIndex = 0
.ShowOpen
End With
If CommonDialog1.Filename <> "" Then
Multimedia.mmOpen CommonDialog1.Filename
Multimedia.mmPlay
End If
End Sub
5. Tambahkan modul (klik kanan pada box project , pilih Add - Module). Masukan listing kode dibawah ini.
Option Explicit
Private sAlias As String
Private sFilename As String
Private nLength As Single
Private nPosition As Single
Private sStatus As String
Private bWait As Boolean
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)
Dim nReturn As Long
Dim sType As String
If sAlias <> "" Then
mmClose
End If
Select Case UCase$(Right$(sTheFile, 3))
Case "WAV"
sType = "Waveaudio"
Case "AVI"
sType = "AviVideo"
Case "MID"
sType = "Sequencer"
Case Else
Exit Sub
End Select
sAlias = Right$(sTheFile, 3) & Minute(Now)
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()
Dim nReturn As Long
If sAlias = "" Then Exit Sub
nReturn = mciSendString("Close " & sAlias, "", 0, 0)
sAlias = ""
sFilename = ""
End Sub
Public Sub mmPause()
Dim nReturn As Long
If sAlias = "" Then Exit Sub
nReturn = mciSendString("Pause " & sAlias, "", 0, 0)
End Sub
Public Sub mmPlay()
Dim nReturn As Long
If sAlias = "" Then Exit Sub
If bWait Then
nReturn = mciSendString("Play " & sAlias & " wait", "", 0, 0)
Else
nReturn = mciSendString("Play " & sAlias, "", 0, 0)
End If
End Sub
Public Sub mmStop()
Dim nReturn As Long
If sAlias = "" Then Exit Sub
nReturn = mciSendString("Stop " & sAlias, "", 0, 0)
End Sub
Public Sub mmSeek(ByVal nPosition As Single)
Dim nReturn As Long
nReturn = mciSendString("Seek " & sAlias & " to " & nPosition, "", 0, 0)
End Sub
Property Get Filename() As String
Filename = sFilename
End Property
Property Let Filename(ByVal sTheFile As String)
mmOpen sTheFile
End Property
Property Get Wait() As Boolean
Wait = bWait
End Property
Property Let Wait(bWaitValue As Boolean)
bWait = bWaitValue
End Property
Property Get Length() As Single
Dim nReturn As Long, nLength As Integer
Dim sLength As String * 255
If sAlias = "" Then
Length = 0
Exit Property
End If
nReturn = mciSendString("Status " & sAlias & " length", sLength, 255, 0)
nLength = InStr(sLength, Chr$(0))
Length = Val(Left$(sLength, nLength - 1))
End Property
Property Let Position(ByVal nPosition As Single)
mmSeek nPosition
End Property
Property Get Position() As Single
Dim nReturn As Integer, nLength As Integer
Dim sPosition As String * 255
If sAlias = "" Then Exit Property
nReturn = mciSendString("Status " & sAlias & " position", sPosition, 255, 0)
nLength = InStr(sPosition, Chr$(0))
Position = Val(Left$(sPosition, nLength - 1))
End Property
Property Get Status() As String
Dim nReturn As Integer, nLength As Integer
Dim sStatus As String * 255
If sAlias = "" Then Exit Property
nReturn = mciSendString("Status " & sAlias & " mode", sStatus, 255, 0)
nLength = InStr(sStatus, Chr$(0))
Status = Left$(sStatus, nLength - 1)
End Property
6. Jalankan dengan menekan tombol F5 pada keyboard anda, dan Selamat sekarang anda mendengar musik yang anda pilih,
Sekian tentang Trik membuat midi Player dengan Visual Basic, Selamat mencoba...
Wassalam.. n smoga bermanfaat..
0 komentar:
Posting Komentar