用VB制作一個簡單的MP3播放器
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、成都網(wǎng)站制作與策劃設(shè)計,橋東網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:橋東等地區(qū)。橋東做網(wǎng)站價格咨詢:18980820575
用VB制作一個簡單的MP3播放器 利用API函數(shù)[mciSendString]可以輕松實現(xiàn)MP3音樂文件的播放。下面這段程序?qū)崿F(xiàn)了MP3播放的大部分常規(guī)操作,對其稍加修改,做一個100KB大小的MP3播放器輕而易舉
啟動VB程序,在窗體上放置6個命令按鈕,三個標簽,一個公用對話框、一個進度條、一個狀態(tài)欄和一個計時器,窗本的布置請參考附圖按鈕"Open MP3 File"是用來打開對話框選擇MP3文件,其他5個按鈕分別為Play(播放)、Pause(暫停)、Stopplay(停止播放)、Back(向后跳躍)、Prew(向前跳躍)。Label1用來表示歌曲當前時間;label2放在時度條的最左邊,Caption屬性為"00:00";lable3放在時度條的右邊,用來表示歌曲總長。下面就可以編寫代碼了。首先在窗體的"通用聲明"部分聲明函數(shù)
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA"(Byval lpstrCommand As String,ByVal lpstrRetumString As String,ByVal uReturnLength As long,ByVal hwndCallback as long) As longDim mfn As String
下面就是各個對象的代碼了:
Private Sub Form_load()
mfn=""
play.Enabled=False
pause.Enabled=False
Stopplay.Enabled=False
back.Enabled=False
prew.Enabled=False
Timer1.Enabled=False
Timer1.Interval=500
End SubPrivate Sub open_Click()
On Error Goto err
With CommonDialog1
.CancelError=True
.Filter="音樂文件|*.mp3;*.wav;*.mid"
.Flags=cdlOFNNoChangeDir And cdlOFNPathMustExist
.Action=1
stopplay_Click '停止按鈕同時可以設(shè)備初始化
mfn=.FileName '這個程序中文件名一定不能帶空格
Form1.Caption=.FileName
play.Enabled=True
End With
play_Click
Exit Sub
err:
End SubPrivate Sub play_Click()
On Error Resume Next
Dim t As Long
t=mciSendString("open " + mfn,0,0,0) 'Open后邊的空格一定不能丟
Dim ret As String * 128
t=mciSendString("status " + mfn + " length",ret,128,0)
'顯示歌曲總長
ret=Left(ret,8)
If ret"" Then
ProgressBar1.Min=0
ProgressBar1.Max=Val(ret)
Label3.Caption=gettime(Val(ret))
End If
play.Enabled=False
pause.Enabled=True
stopplay.Enabled=True
back.Enabled=True
prew.Enabled=True
t=mciSendString("status " + mfn +" mode",ret,128,0)
'得到設(shè)備的當前狀態(tài),是播放還是暫停等等
ret=Left(ret,8)
StatusBar1.Panels(1).text=ret
'在狀態(tài)欄顯示播放狀態(tài)
t=mciSendString("play " + mfn + " form " + Str(ProgressBar1.Value),0,0,0)
'開始播放
Timer1.Enabled=True
End Sub
Private Sub pause_Click()
t%=mciSendString("pause " + mfn,0,0,0)
'發(fā)出暫停的命令
play.Enable=True
pause.Enabled=False
stopplay.Enabled=True
back.Enabled=False
prew.Enabled=False
End Sub
Private Sub stopplay_Click()
t%=mciSendString("stop " + mfn,0,0,0)
t%=mciSendString("close " + mfn,0,0,0)
'停止播放
play.Enabled=True
pause.Enabled=False
stopplay.Enabled=False
back.Enabled=False
prew.Enabled=False
End Sub
Private Sub back_Click()
t%=mciSendString("play " + mfn + " from " + Str(ProgressBar1.Value-(ProgressBar1.Max\10)),0,0,0) '向后跳一小段再播放
End Sub
Private Sub prew_Click()
t%=mciSendString("play " + mfn + " from " + Stri(ProgressBar1.Value+(ProgressBar1.Max\10)),0,0,0) '向前跳一小段再播放
End Sub
Private Sub Timer1_Timer()
Dim t As Long
Dim ret As String * 128
t=mciSendString("status " + mfn + " position",ret,0,0)
'得到當前播放位置
ret=left(ret,8)
ProgressBar1.Value=Val(ret)
Label1.Caption=gettime(Val(ret))
'顯示歌曲當前時間
If ProgressBar1.Value=ProgressBar1.Max Then
stopplay_Click
End If
t=mciSendString("status " + mfn + " mode",ret,128,0)
ret=Left(ret,8)
StatusBar1.Panels(1).Text=ret
End SubPrivate Sub Form_Unload(Cacel As Integer)
t%=mciSendString("stop " + mfn,0,0,0)
t%=mciSendString("close " + mfn,0,0,0)
End SubPrivate Function Gettime(position As Long) As String
'這個函數(shù)的功能是把以長整型表示的時間轉(zhuǎn)換為電子鐘式的"**:**"
Dim min,sec
min=position/1000
min=min/60
sec=min-Int(min)
min=Int(min)
sec=60 * sec / 100
sec=Int(sec * 100)
gettime=Str(min) + ":" + Str(sec)
End Function
Public Class Form1
Dim soundname As String()
Dim i As Integer
Dim j, l As Boolean
Dim k As Integer
Dim filename As String '取歌曲的名字
Dim path As String()
Dim s, m As Integer
Dim luj As String
Dim count As Integer '用來聲明選擇歌曲的數(shù)目
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
selectsound()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics
g = Graphics.FromHwnd(Me.Handle)
Dim mfont As New Font("宋體", 20)
Dim mstringformat As New StringFormat
mstringformat.Alignment = StringAlignment.Center
mstringformat.LineAlignment = StringAlignment.Center
g.DrawString("X071616", mfont, Brushes.Black, 75, 328, mstringformat)
If ListBox1.Items.Count = 0 Then selectsound() : Exit Sub
playsound()
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Me.AxWindowsMediaPlayer1.settings.volume = TrackBar1.Value
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Timer1.Enabled = False Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
If i = 0 Then
Me.AxWindowsMediaPlayer1.Ctlcontrols.pause()
i = 1
Button3.Text = "繼續(xù)"
Exit Sub
End If
If i = 1 Then
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
Button3.Text = "暫停"
i = 0
Exit Sub
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
j = True
playsound()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim string1, ss, sss As String
string1 = ListBox1.SelectedItem.ToString.Substring(ListBox1.SelectedItem.ToString.LastIndexOf("\") + 1)
filename = string1.Substring(0, string1.LastIndexOf("."))
luj = ListBox1.SelectedItem.ToString.Substring(0, ListBox1.SelectedItem.ToString.LastIndexOf(".")) + ".lrc"
Me.HScrollBar1.Value = Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition / (Me.AxWindowsMediaPlayer1.currentMedia.duration + 0.001) * 1000
ss = Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
Me.Label1.Text = ss
If Dir(luj) Nothing Then
Dim fr As System.IO.StreamReader = New System.IO.StreamReader(luj, System.Text.Encoding.Default)
sss = fr.ReadLine '我們讀取的第一行歌詞
While (sss "")
If (sss.LastIndexOf(ss) -1) Then '時間匹配了歌詞中的時間
Me.TextBox1.Text = sss.Substring(sss.LastIndexOf("]") + 1)
Form2.TextBox1.Text = Me.TextBox1.Text
End If
sss = fr.ReadLine
End While
Else
TextBox1.Text = "Not Find! 請從網(wǎng)上下載歌詞!"
Form2.TextBox1.Text = "Not Find! 請從網(wǎng)上下載歌詞!"
End If
Label1.Text = "當前進度:" + Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = Me.HScrollBar1.Value / 1000 * Me.AxWindowsMediaPlayer1.currentMedia.duration
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Me.AxWindowsMediaPlayer1.Ctlcontrols.fastForward()
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Me.AxWindowsMediaPlayer1.Ctlcontrols.fastReverse()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
i = 0
j = False
l = False
Label2.Text = "歌曲名: "
Label3.Text = "歌曲演唱者: "
Label4.Text = "歌曲描述: "
Label5.Text = "歌曲類型: "
Label6.Text = "歌曲大?。?"
Button8.Enabled = False
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(ListBox1.SelectedIndex)
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
Label2.Text = "歌曲名: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Title")
Label3.Text = "歌曲演唱者: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Author")
Label4.Text = "歌曲描述: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Description")
Label5.Text = "歌曲類型: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileType")
Label6.Text = "歌曲大?。?" + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize")
Timer1.Start()
End Sub
Private Sub playsound()
Timer1.Stop()
TrackBar1.Value = Me.AxWindowsMediaPlayer1.settings.volume
If j = False Then
Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(ListBox1.SelectedIndex)
ElseIf j = True Then
k = (ListBox1.SelectedIndex + 1) Mod soundname.Length
Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(k)
j = False
End If
ListBox1.SelectedIndex = k
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
Label2.Text = "歌曲名: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Title")
Label3.Text = "歌曲演唱者: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Author")
Label4.Text = "歌曲描述: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Description")
Label5.Text = "歌曲類型: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileType")
Label6.Text = "歌曲大?。?" + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize")
Timer1.Start()
End Sub
Private Sub selectsound()
Dim open As New OpenFileDialog
Dim i As Integer
With open
.Filter = "所有mp3文件(*.mp3)|*.mp3|所有wma文件(*.wma)|*.wma"
.Multiselect = True
.Title = "請選擇歌曲"
End With
If (open.ShowDialog = Windows.Forms.DialogResult.OK) Then
soundname = open.FileNames
For i = 0 To soundname.GetUpperBound(0)
ListBox1.Items.Add(soundname(i))
Next
ListBox1.SelectedIndex = 0
End If
End Sub
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
j = True
Timer2.Start()
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
playsound()
Timer2.Stop()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Timer1.Stop()
If ListBox1.SelectedIndex = 0 Then
Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(soundname.Length - 1)
ListBox1.SelectedIndex = (soundname.Length - 1)
Else
Me.AxWindowsMediaPlayer1.URL = ListBox1.Items((ListBox1.SelectedIndex Mod soundname.Length) - 1)
ListBox1.SelectedIndex = (ListBox1.SelectedIndex Mod soundname.Length) - 1
End If
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
Label2.Text = "歌曲名: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Title")
Label3.Text = "歌曲演唱者: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Author")
Label4.Text = "歌曲描述: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Description")
Label5.Text = "歌曲類型: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileType")
Label6.Text = "歌曲大?。?" + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize")
Timer1.Start()
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Form2.Show()
End Sub
End Class
先定義api函數(shù)
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrRetumString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Sub mnu_Music_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_Music.Click
mciSendString("open c:\1.mp3 alias mp3", Nothing, 0, IntPtr.Zero)
mciSendString("Play mp3", Nothing, 0, IntPtr.Zero)
End Sub
上面2行代碼就可以播放了,測試通過。
就是沒有其它功能,自己加上吧,
如:關(guān)閉 mciSendString("close mp3",Nothing,0,intPtr.Zero)
等等
工具欄里選擇"添加,刪除組件"
選擇com組件
選擇"Windows Media Player "
然后在界面上加入這個組件
代碼
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Const DATA_FILE_EXTENSION As String = ".mp3"
Dim dlgFileDialog As New OpenFileDialog
With dlgFileDialog
.Filter = DATA_FILE_EXTENSION _
" files (*" DATA_FILE_EXTENSION "|*" DATA_FILE_EXTENSION
.FilterIndex = 1
.RestoreDirectory = True
If .ShowDialog() = DialogResult.OK Then
'Play the sound file
Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName
End If
End With
End Sub
參考資料中可以看到很詳細的步驟
在.NET里,沒有托管的音樂播放器,用API只能播放WAV格式,對于MP3等形式的音頻文件,就要依賴于其他控件了,常用的就是
MediaPlayer。使用方法:
在工具箱上點右鍵,選擇“選擇項目(Choose
Items)”,切到COM選項卡,找到
Windows
Media
Player,
勾選,確定
在t工具箱上,把剛才加入的MediaPlayer控件,拖放到窗體上
代碼:
WindowsMediaPlayer1.URL
=
"mp3文件路徑,支持網(wǎng)絡(luò)路徑"
WindowsMediaPlayer1.Ctlcontrols.play
基本就OK了,至于其他的操作諸如下一曲上一曲暫停停止,直接看
WindowsMediaPlayer1.Ctlcontrols.
里面的方法吧。
這是我的代碼:
CommonDialog1.Filter = "All Files (*.*)|*.*|Windows Media 格式文檔 (*.asf;*.wma;*.wmv;*.wm)|*.asf;*.wma;*.wmv;*.wm|Windows Media 音頻 (*.wma)|*.wma|Windows Media 視頻 (*.wmv;*.wm)|*.wmv;*.wm|Windows Media 元文件 (*.asx;*.wax;*.wvx;*.wpl)|*.asx;*.wax;*.wvx;*.wpl|Windows Media 音頻重定向器 (*.wax)|*.wax|Windows Media 視頻重定向器 (*.wvx)|*.wvx"
CommonDialog1.ShowOpen
WindowsMediaPlayer1.URL = CommonDialog1.FileName
'這只是打開文件的代碼哦
--------------------------------------------------------------------------------------------------
控制播放器的代碼:
WindowsMediaPlayer1.play'控制播放器開始播放
WindowsMediaPlayer1.stop'控制播放器暫停播放