思路很簡(jiǎn)單可以定義一個(gè)結(jié)構(gòu)LyricStructure Lyric
創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供上虞網(wǎng)站建設(shè)、上虞做網(wǎng)站、上虞網(wǎng)站設(shè)計(jì)、上虞網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、上虞企業(yè)網(wǎng)站模板建站服務(wù),10年上虞做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
Dim Time As Single
Dim Content As String
End Structure聲明一個(gè)列表 Private Lyrics As New List(Of Lyric)然后以文本方式把歌詞文件讀入一個(gè)變量 并對(duì)它進(jìn)行分析 分別把時(shí)間和歌詞內(nèi)容存入一個(gè)Lyric,并加入Lyrics中。代碼如下:(假設(shè)歌曲文件的路徑為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 '長(zhǎng)度小于5則跳過(guò),進(jìn)行下一次循環(huán)
Select Case Mid(tmpStr, 2, 2) '判斷第2-3個(gè)字符 因?yàn)楦柙~里的注釋都是[ti:***]類(lèi)似的結(jié)構(gòu)
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 '如果不為以上的內(nèi)容 則為歌詞正文,進(jìn)行以下操作
If Not Mid(tmpStr, 1, 6) Like "?##:##" Then Continue Do
Dim time(-1) As String '因?yàn)橛袝r(shí)歌詞是以[00:00.00][00:00.00]**** 這樣歌詞把多個(gè)時(shí)間存放在一起的 ,所以定義一個(gè)數(shù)組來(lái)存放歌詞
Do '因?yàn)椴淮_定時(shí)間的個(gè)數(shù),所以此處必須循環(huán)判斷
tm = InStr(tmpStr, "]") '因?yàn)楦柙~時(shí)間有時(shí)是[00:00.00]形式,有時(shí)是[00:00]形式,所以不能單純的截取第幾個(gè)字符 ,我是根據(jù)查找']'字符來(lái)進(jìn)行判斷的
ReDim Preserve time(time.GetUpperBound(0) + 1) 為time數(shù)組重新分配大小
time(time.GetUpperBound(0)) = Mid(tmpStr, 1, tm) 'time.GetUpperBound(0)返回?cái)?shù)組的第一維的最大下標(biāo)
tmpStr = IIf(tmpStr.Length = tm, " ", Mid(tmpStr, tm + 1)) '歌詞內(nèi)容
Loop While Mid(tmpStr, 1, 6) Like "?##:##" '如果截取剩下的部分依然匹配的話繼續(xù)循環(huán)
For Each t In time '為每個(gè)時(shí)間都創(chuàng)建一個(gè)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() 關(guān)閉流
Sort() '把所有Lyric按時(shí)間順序排序
End Sub Private Function SwitchTime(ByVal time As String) As Single ‘這個(gè)函數(shù)是用來(lái)把00:00.00格式的時(shí)間轉(zhuǎn)化成以秒數(shù)顯示的數(shù)值
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 接下來(lái)是顯示歌詞的問(wèn)題了 ,只要有每句歌詞和對(duì)應(yīng)的時(shí)間,那么問(wèn)題就好辦了。你可以用Windows Media Player控件返回的時(shí)間和每句歌詞的時(shí)間做比較,來(lái)判斷應(yīng)該顯示那一句。代碼是很久前寫(xiě)的 ,那時(shí)沒(méi)加注釋?zhuān)际莿倓偧由先サ膯眩憧梢愿鶕?jù)你的實(shí)際情況自己修改,有什么不懂的地方可以繼續(xù)問(wèn)我哈~~
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 '用來(lái)聲明選擇歌曲的數(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 '時(shí)間匹配了歌詞中的時(shí)間
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! 請(qǐng)從網(wǎng)上下載歌詞!"
Form2.TextBox1.Text = "Not Find! 請(qǐng)從網(wǎng)上下載歌詞!"
End If
Label1.Text = "當(dāng)前進(jìn)度:" + 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 = "歌曲類(lèi)型: "
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 = "歌曲類(lèi)型: " + 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 = "歌曲類(lèi)型: " + 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 = "請(qǐng)選擇歌曲"
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 = "歌曲類(lèi)型: " + 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
可以稍微改下,預(yù)讀兩句歌詞的時(shí)間范圍,時(shí)間大于第一句的起始點(diǎn),小于第二句的起始點(diǎn),那么顯示第一句,