อันนี้ผมถอดรหัสไฟล์ WMPLib.dll นำฟังก์ชั่นคำสั่งออกมาใช้บางส่วน
และใช้เวลาเขียนออกมาเป็นโปรแกรมสำหรับฟังเพลง ในเวลาไม่นานนัก
ท่านลองนำไปศึกษาดู ติดขัดตรงไหนก็โฟสท์ถาม
WMP Player
Public Class Form1
Private button1 As New System.Windows.Forms.Button
Private button2 As New System.Windows.Forms.Button
Private button3 As New System.Windows.Forms.Button
Private fdlg As New System.Windows.Forms.OpenFileDialog
Private label1 As New System.Windows.Forms.Label
Private label2 As New System.Windows.Forms.Label
Private label3 As New System.Windows.Forms.Label
Private label4 As New System.Windows.Forms.Label
Private label5 As New System.Windows.Forms.Label
Private media As New WMPLib.WindowsMediaPlayer
Private timer1 As New System.Windows.Forms.Timer
Private fileName As String
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
Me.Size = New Size(300, 170)
Me.Text = "WMP Player"
Me.MaximizeBox = False
Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
button1.Text = "Open"
button2.Text = "Pause"
button3.Text = "Stop"
button1.Cursor = Cursors.Hand
button2.Cursor = Cursors.Hand
button3.Cursor = Cursors.Hand
label1.Text = "00:00"
label2.Text = "00:00"
label3.Text = "No Song"
label4.Text = "Stopped"
label5.Text = "0%"
button1.Location = New System.Drawing.Point(27, 100)
button2.Location = New System.Drawing.Point(107, 100)
button3.Location = New System.Drawing.Point(187, 100)
label1.Location = New System.Drawing.Point(30, 10)
label2.Location = New System.Drawing.Point(225, 10)
label3.Location = New System.Drawing.Point(90, 43)
label4.Location = New System.Drawing.Point(110, 68)
label5.Location = New System.Drawing.Point(130, 10)
Me.Controls.AddRange(New Control() _
{button1, button2, button3, _
label1, label2, label3, label4, label5})
AddHandler button1.Click, AddressOf Button1_Click
AddHandler Button2.Click, AddressOf Button2_Click
AddHandler button3.Click, AddressOf Button3_Click
AddHandler timer1.Tick, AddressOf Timer1_Tick
timer1.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Button2.Text = "Resume" Then Button2.Text = "Pause"
LoadPlaySound()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
SoundPause()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If button2.Text = "Resume" Then button2.Text = "Pause"
SoundStop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
label1.Text = media.currentMedia.durationString
label2.Text = media.controls.currentPositionString
label5.Text = Math.Round((media.controls.currentPosition / media.currentMedia.duration) * 100, 0) & "%"
Select Case media.playState
Case WMPLib.WMPPlayState.wmppsPlaying
label4.Text = "Playing.."
Case WMPLib.WMPPlayState.wmppsPaused
label4.Text = "Paused"
Case WMPLib.WMPPlayState.wmppsStopped
label2.Text = "00:00"
label4.Text = "Song end"
label5.Text = "0%"
End Select
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If timer1.Enabled = True Then timer1.Enabled = False
media.close()
End Sub
Public Sub LoadPlaySound()
Select Case button1.Text
Case "Open"
fdlg.FileName = ""
fdlg.Filter = "Media Files(*.mp3;*.wav)|*.mp3;*.wav|All files(*.*)|*.*"
If fdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
fileName = fdlg.FileName
button1.Text = "Play"
label3.Text = My.Computer.FileSystem.GetName(fileName)
Else
If fileName = Nothing Then Exit Sub
button1.Text = "Play"
End If
Case "Play"
media.URL = fileName
timer1.Enabled = True
End Select
End Sub
Public Sub SoundPause()
Select Case button2.Text
Case "Pause"
If media.playState = WMPLib.WMPPlayState.wmppsPlaying Then
media.controls.pause()
button2.Text = "Resume"
End If
Case "Resume"
If media.playState = WMPLib.WMPPlayState.wmppsPaused Then
media.controls.play()
button2.Text = "Pause"
End If
End Select
End Sub
Public Sub SoundStop()
Select Case media.playState
Case WMPLib.WMPPlayState.wmppsPaused
label2.Text = "00:00"
label4.Text = "Stopped"
label5.Text = "0%"
button1.Text = "Open"
timer1.Enabled = False
Case WMPLib.WMPPlayState.wmppsPlaying
media.controls.stop()
label2.Text = "00:00"
label4.Text = "Stopped"
label5.Text = "0%"
button1.Text = "Open"
timer1.Enabled = False
Case WMPLib.WMPPlayState.wmppsStopped
If label4.Text = "Stopped" Then
label2.Text = "00:00"
label5.Text = "0%"
timer1.Enabled = False
End If
End Select
End Sub
End Class