大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用VB制作一个简单的MP3播放器
创新互联是一家专注于成都网站设计、成都网站制作与策划设计,桥东网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:桥东等地区。桥东做网站价格咨询:18980820575
用VB制作一个简单的MP3播放器 利用API函数[mciSendString]可以轻松实现MP3音乐文件的播放。下面这段程序实现了MP3播放的大部分常规操作,对其稍加修改,做一个100KB大小的MP3播放器轻而易举
启动VB程序,在窗体上放置6个命令按钮,三个标签,一个公用对话框、一个进度条、一个状态栏和一个计时器,窗本的布置请参考附图按钮"Open MP3 File"是用来打开对话框选择MP3文件,其他5个按钮分别为Play(播放)、Pause(暂停)、Stopplay(停止播放)、Back(向后跳跃)、Prew(向前跳跃)。Label1用来表示歌曲当前时间;label2放在时度条的最左边,Caption属性为"00:00";lable3放在时度条的右边,用来表示歌曲总长。下面就可以编写代码了。首先在窗体的"通用声明"部分声明函数
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 '停止按钮同时可以设备初始化
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)
'得到设备的当前状态,是播放还是暂停等等
ret=Left(ret,8)
StatusBar1.Panels(1).text=ret
'在状态栏显示播放状态
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)
'发出暂停的命令
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
'这个函数的功能是把以长整型表示的时间转换为电子钟式的"**:**"
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 '用来声明选择歌曲的数目
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 = "继续"
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! 请从网上下载歌词!"
Form2.TextBox1.Text = "Not Find! 请从网上下载歌词!"
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函数
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行代码就可以播放了,测试通过。
就是没有其它功能,自己加上吧,
如:关闭 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文件路径,支持网络路径"
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'控制播放器暂停播放