包含vb.netlrc的词条
关于播放器中的功能(歌词同步),急求VB.NET代码!!
思路很简单可以定义一个结构LyricStructure Lyric
创新互联建站专业为企业提供上虞网站建设、上虞做网站、上虞网站设计、上虞网站制作等企业网站建设、网页设计与制作、上虞企业网站模板建站服务,10年上虞做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
Dim Time As Single
Dim Content As String
End Structure声明一个列表 Private Lyrics As New List(Of Lyric)然后以文本方式把歌词文件读入一个变量 并对它进行分析 分别把时间和歌词内容存入一个Lyric,并加入Lyrics中。代码如下:(假设歌曲文件的路径为path)Dim LrcPath As String== IO.Path.GetDirectoryName(cPath) "/" IO.Path.GetFileNameWithoutExtension(cPath) ".lrc"If IO.File.Exists(LrcPath= True Then AnalysisLyric() Private Sub AnalysisLyric()
Lyrics.Clear() '先清空列表
Dim tm As Byte
Dim sr As New IO.StreamReader(LrcPath, System.Text.Encoding.Default) '读取歌词文件
Do While sr.EndOfStream = False '判断是否处于文件流末尾
Dim tmpStr As String = sr.ReadLine '读取一行
If tmpStr.Length 5 Then Continue Do '长度小于5则跳过,进行下一次循环
Select Case Mid(tmpStr, 2, 2) '判断第2-3个字符 因为歌词里的注释都是[ti:***]类似的结构
Case "ti"
lTitle = Mid(tmpStr, 5, tmpStr.Length - 5)
Case "ar"
lArtist = Mid(tmpStr, 5, tmpStr.Length - 5)
Case "al"
lAlbum = Mid(tmpStr, 5, tmpStr.Length - 5)
Case "by"
lAuthor = Mid(tmpStr, 5, tmpStr.Length - 5)
Case Else '如果不为以上的内容 则为歌词正文,进行以下操作
If Not Mid(tmpStr, 1, 6) Like "?##:##" Then Continue Do
Dim time(-1) As String '因为有时歌词是以[00:00.00][00:00.00]**** 这样歌词把多个时间存放在一起的 ,所以定义一个数组来存放歌词
Do '因为不确定时间的个数,所以此处必须循环判断
tm = InStr(tmpStr, "]") '因为歌词时间有时是[00:00.00]形式,有时是[00:00]形式,所以不能单纯的截取第几个字符 ,我是根据查找']'字符来进行判断的
ReDim Preserve time(time.GetUpperBound(0) + 1) 为time数组重新分配大小
time(time.GetUpperBound(0)) = Mid(tmpStr, 1, tm) 'time.GetUpperBound(0)返回数组的第一维的最大下标
tmpStr = IIf(tmpStr.Length = tm, " ", Mid(tmpStr, tm + 1)) '歌词内容
Loop While Mid(tmpStr, 1, 6) Like "?##:##" '如果截取剩下的部分依然匹配的话继续循环
For Each t In time '为每个时间都创建一个Lyric变量
Dim ln As Lyric
ln.Time = SwitchTime(Mid(t, 2, t.Length - 2))
ln.Content = tmpStr
Lyrics.Add(ln)
Next
End Select
Loop
sr.Close() 关闭流
Sort() '把所有Lyric按时间顺序排序
End Sub Private Function SwitchTime(ByVal time As String) As Single ‘这个函数是用来把00:00.00格式的时间转化成以秒数显示的数值
Dim tm As Single
Dim minute As Single = Val(Mid(time, 1, 2))
Dim second As Single = Val(Mid(time, 4, 5))
tm = minute * 60 + second
Return tm
End Function Private Sub Sort()
For i = 0 To Lyrics.Count - 2
For j = i To Lyrics.Count - 1
If Lyrics(i).Time Lyrics(j).Time Then
Dim tm As Lyric = Lyrics(i)
Lyrics(i) = Lyrics(j)
Lyrics(j) = tm
End If
Next
Next
End Sub 接下来是显示歌词的问题了 ,只要有每句歌词和对应的时间,那么问题就好办了。你可以用Windows Media Player控件返回的时间和每句歌词的时间做比较,来判断应该显示那一句。代码是很久前写的 ,那时没加注释,都是刚刚加上去的哟,你可以根据你的实际情况自己修改,有什么不懂的地方可以继续问我哈~~
使用VB.NET制作带歌词显示的mp3播放器
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
VB.NET 怎么同步显示lrc歌词??
可以稍微改下,预读两句歌词的时间范围,时间大于第一句的起始点,小于第二句的起始点,那么显示第一句,
标题名称:包含vb.netlrc的词条
本文网址:http://myzitong.com/article/hoieeo.html