VB.net写的音乐播放器,带百度翻译歌词
生活随笔
收集整理的這篇文章主要介紹了
VB.net写的音乐播放器,带百度翻译歌词
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
?
Imports System.Drawing.Text Imports System.IOPublic Class Form1Public RND As New RandomPublic g As BufferedGraphicsPublic g2 As BufferedGraphicsPublic fonts20 As New Font("微軟雅黑", 20)Public fonts11 As New Font("宋體", 9)Public 居中畫字樣式 As StringFormatPublic 播放器 As 歌曲Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loadg = BufferedGraphicsManager.Current.Allocate(PictureBox1.CreateGraphics, PictureBox1.DisplayRectangle)g2 = BufferedGraphicsManager.Current.Allocate(PictureBox2.CreateGraphics, PictureBox2.DisplayRectangle)讀取音樂(lè)文件并且刷新listview1()居中畫字樣式 = New StringFormat()居中畫字樣式.LineAlignment = StringAlignment.Center居中畫字樣式.Alignment = StringAlignment.CenterEnd SubPublic gequ As New Generic.Dictionary(Of String, String)Public Sub 讀取音樂(lè)文件并且刷新listview1()ListView1.BeginUpdate()ListView1.Items.Clear()Dim files As String() = Directory.GetFiles("C:\Users\panwei\Desktop\11")' Dim files As String() = Directory.GetFiles(Application.StartupPath)For Each file As String In filesIf Path.GetExtension(file) = ".mp3" ThenDim 歌曲名 = Path.GetFileNameWithoutExtension(file)gequ.Add(file, 歌曲名)Dim t As ListViewItem = New ListViewItem({歌曲名})t.Tag = filet.BackColor = Color.FromArgb(RND.Next(200, 255), RND.Next(200, 255), RND.Next(200, 255))ListView1.Items.Add(t)End IfNextListView1.EndUpdate()End SubPrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.TickIf 播放器 IsNot Nothing Theng.Graphics.Clear(Color.Black)g.Graphics.FillRectangle(Brushes.Green, New RectangleF(0, 0, 播放器.當(dāng)前播放時(shí)間_百分比 * PictureBox1.Width, PictureBox1.Height))'繪制時(shí)間g.Graphics.DrawString(播放器.當(dāng)前播放時(shí)間 & "/" & 播放器.歌曲長(zhǎng)度, fonts20, Brushes.Gold, 0, 0)'繪制歌詞For I = 1 To 播放器.歌詞.Count - 2If 播放器.歌詞(I + 1).歌詞時(shí)間 > 播放器.當(dāng)前播放時(shí)間 Theng.Graphics.DrawString(播放器.歌詞(I).歌詞文本, fonts20, Brushes.Gold, New Rectangle(Point.Empty, PictureBox1.Size), 居中畫字樣式)Exit ForEnd IfNextg.Render()'繪制歌詞g2.Graphics.Clear(Color.Black)Dim 當(dāng)前歌詞 As Boolean = FalseFor I = 0 To 播放器.歌詞.Count - 1If I <> 播放器.歌詞.Count - 1 AndAlso 播放器.歌詞(I + 1).歌詞時(shí)間 > 播放器.當(dāng)前播放時(shí)間 And 當(dāng)前歌詞 = False ThenDim se = 播放器.歌詞(I).顏色Dim se2 = Color.FromArgb(se.R - 100, se.G - 100, se.B - 100)Dim 當(dāng)前歌詞長(zhǎng)度 = 播放器.歌詞(I + 1).歌詞時(shí)間 - 播放器.歌詞(I).歌詞時(shí)間Dim 這句歌詞現(xiàn)在播放到 = 播放器.當(dāng)前播放時(shí)間 - 播放器.歌詞(I).歌詞時(shí)間Dim kd = (PictureBox2.Width / 3) / 50For N = 0 To (50 * (這句歌詞現(xiàn)在播放到 / 當(dāng)前歌詞長(zhǎng)度))g2.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255 - 5 * N, 5 * N, 0)), New RectangleF((I Mod 3) * (PictureBox2.Width / 3) + N * kd, Int(I / 3) * 當(dāng)前繪制高度, kd - 2, 當(dāng)前繪制高度))Nextg2.Graphics.DrawString(播放器.歌詞(I).歌詞文本, fonts11, Brushes.White, New Rectangle((I Mod 3) * (PictureBox2.Width / 3), Int(I / 3) * 當(dāng)前繪制高度, (PictureBox2.Width / 3), 當(dāng)前繪制高度), 居中畫字樣式)當(dāng)前歌詞 = TrueElseg2.Graphics.FillRectangle(New SolidBrush(播放器.歌詞(I).顏色), New Rectangle((I Mod 3) * (PictureBox2.Width / 3), Int(I / 3) * 當(dāng)前繪制高度, (PictureBox2.Width / 3), 當(dāng)前繪制高度))g2.Graphics.DrawString(播放器.歌詞(I).歌詞文本, fonts11, Brushes.Black, New Rectangle((I Mod 3) * (PictureBox2.Width / 3), Int(I / 3) * 當(dāng)前繪制高度, (PictureBox2.Width / 3), 當(dāng)前繪制高度), 居中畫字樣式)End IfNextg2.Render()End IfEnd SubPublic 當(dāng)前繪制高度 As Integer = 35Private Sub 進(jìn)度條彈起(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUpIf 播放器 IsNot Nothing Then播放器.當(dāng)前播放時(shí)間_百分比 = e.X / PictureBox1.Width播放器.播放狀態(tài) = 歌曲.播放.播放End IfEnd SubPrivate Sub 歌詞彈起(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUpDim X As Integer = Int(e.X / (PictureBox2.Width / 3))Dim Y As Integer = Int(e.Y / 當(dāng)前繪制高度)Dim INDEX = X + Y * 3If INDEX < 播放器.歌詞.Count Then播放器.當(dāng)前播放時(shí)間 = 播放器.歌詞(INDEX).歌詞時(shí)間播放器.播放音樂(lè)()End IfEnd SubPrivate Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChangedIf ListView1.SelectedItems.Count >= 1 ThenIf 播放器 IsNot Nothing Then播放器.close()播放器 = NothingEnd If播放器 = New 歌曲(ListView1.SelectedItems(0).Tag)播放器.播放音樂(lè)()當(dāng)前繪制高度 = PictureBox2.Height / (播放器.歌詞.Count / 3 + 1)End IfEnd SubPrivate Sub 上一首(sender As Object, e As EventArgs) Handles Button2.ClickTryIf ListView1.SelectedItems.Count >= 1 ThenIf ListView1.SelectedItems(0).Index >= 1 ThenDim ID = ListView1.SelectedItems(0).Index - 1ListView1.SelectedItems.Clear()ListView1.Items(ID).Selected = TrueListView1.Focus()End IfEnd IfCatch ex As ExceptionEnd TryEnd SubPrivate Sub 下一首(sender As Object, e As EventArgs) Handles Button3.Click'TryIf ListView1.SelectedItems.Count >= 1 ThenIf ListView1.SelectedItems(0).Index < ListView1.Items.Count - 1 ThenDim ID = ListView1.SelectedItems(0).Index + 1ListView1.SelectedItems.Clear()ListView1.Items(ID).Selected = TrueListView1.Focus()End IfEnd If ' Catch ex As Exception' End TryEnd SubPrivate Sub 播放or暫停(sender As Object, e As EventArgs) Handles Button1.ClickTryIf 播放器.播放狀態(tài) = 歌曲.播放.停止 Or 播放器.播放狀態(tài) = 歌曲.播放.暫停 ThenButton1.Text = "暫停"播放器.播放狀態(tài) = 歌曲.播放.播放ElseIf 播放器.播放狀態(tài) = 歌曲.播放.播放 ThenButton1.Text = "播放"播放器.播放狀態(tài) = 歌曲.播放.暫停End IfCatch ex As ExceptionEnd TryEnd SubPrivate Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.ClickEnd SubPrivate Sub SplitContainer3_Panel2_Paint(sender As Object, e As PaintEventArgs) Handles SplitContainer3.Paintg = BufferedGraphicsManager.Current.Allocate(PictureBox1.CreateGraphics, PictureBox1.DisplayRectangle)g2 = BufferedGraphicsManager.Current.Allocate(PictureBox2.CreateGraphics, PictureBox2.DisplayRectangle)If 播放器 IsNot Nothing Then當(dāng)前繪制高度 = PictureBox2.Height / (播放器.歌詞.Count / 3 + 1)End IfListView1.Columns(0).Width = ListView1.WidthEnd SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickIf 播放器 IsNot Nothing Then播放器.翻譯英文()End IfMsgBox("翻譯成功")End SubPrivate Sub ListView1_MouseUp(sender As Object, e As MouseEventArgs) Handles ListView1.MouseUpIf e.Button = MouseButtons.Right ThenContextMenuStrip1.Show(Cursor.Position)End IfEnd SubPrivate Sub 刪除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 刪除ToolStripMenuItem.ClickIf ListView1.SelectedItems.Count >= 1 ThenDim FILE = ListView1.SelectedItems(0).TagListView1.SelectedItems(0).Remove()IO.File.Delete(FILE)End IfEnd SubPrivate Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChangedListView1.BeginUpdate()If TextBox1.Text = "" ThenListView1.Items.Clear()For Each I In gequDim T As New ListViewItem(I.Value)T.BackColor = Color.FromArgb(RND.Next(200, 255), RND.Next(200, 255), RND.Next(200, 255))T.Tag = I.KeyListView1.Items.Add(T)NextElseListView1.Items.Clear()For Each I In gequIf InStr(I.Value, TextBox1.Text) ThenDim T As New ListViewItem(I.Value)T.BackColor = Color.FromArgb(RND.Next(200, 255), RND.Next(200, 255), RND.Next(200, 255))T.Tag = I.KeyListView1.Items.Add(T)End IfNextEnd IfListView1.EndUpdate()End Sub End Class?
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> Partial Class Form1Inherits System.Windows.Forms.Form'Form 重寫 Dispose,以清理組件列表。<System.Diagnostics.DebuggerNonUserCode()>Protected Overrides Sub Dispose(ByVal disposing As Boolean)TryIf disposing AndAlso components IsNot Nothing Thencomponents.Dispose()End IfFinallyMyBase.Dispose(disposing)End TryEnd Sub'Windows 窗體設(shè)計(jì)器所必需的Private components As System.ComponentModel.IContainer'注意: 以下過(guò)程是 Windows 窗體設(shè)計(jì)器所必需的'可以使用 Windows 窗體設(shè)計(jì)器修改它。 '不要使用代碼編輯器修改它。<System.Diagnostics.DebuggerStepThrough()>Private Sub InitializeComponent()Me.components = New System.ComponentModel.Container()Me.PictureBox1 = New System.Windows.Forms.PictureBox()Me.Timer1 = New System.Windows.Forms.Timer(Me.components)Me.PictureBox2 = New System.Windows.Forms.PictureBox()Me.Button1 = New System.Windows.Forms.Button()Me.Button2 = New System.Windows.Forms.Button()Me.Button3 = New System.Windows.Forms.Button()Me.SplitContainer1 = New System.Windows.Forms.SplitContainer()Me.SplitContainer2 = New System.Windows.Forms.SplitContainer()Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()Me.Button4 = New System.Windows.Forms.Button()Me.SplitContainer3 = New System.Windows.Forms.SplitContainer()Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)Me.刪除ToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()Me.TextBox1 = New System.Windows.Forms.TextBox()Me.ListView1 = New System.Windows.Forms.ListView()Me.ColumnHeader1 = CType(New System.Windows.Forms.ColumnHeader(), System.Windows.Forms.ColumnHeader)CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).BeginInit()CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).BeginInit()Me.SplitContainer1.Panel1.SuspendLayout()Me.SplitContainer1.Panel2.SuspendLayout()Me.SplitContainer1.SuspendLayout()CType(Me.SplitContainer2, System.ComponentModel.ISupportInitialize).BeginInit()Me.SplitContainer2.Panel1.SuspendLayout()Me.SplitContainer2.Panel2.SuspendLayout()Me.SplitContainer2.SuspendLayout()Me.FlowLayoutPanel1.SuspendLayout()CType(Me.SplitContainer3, System.ComponentModel.ISupportInitialize).BeginInit()Me.SplitContainer3.Panel1.SuspendLayout()Me.SplitContainer3.Panel2.SuspendLayout()Me.SplitContainer3.SuspendLayout()Me.ContextMenuStrip1.SuspendLayout()Me.SuspendLayout()''PictureBox1'Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.FillMe.PictureBox1.Location = New System.Drawing.Point(0, 0)Me.PictureBox1.Name = "PictureBox1"Me.PictureBox1.Size = New System.Drawing.Size(1130, 96)Me.PictureBox1.TabIndex = 6Me.PictureBox1.TabStop = False''Timer1'Me.Timer1.Enabled = TrueMe.Timer1.Interval = 10''PictureBox2'Me.PictureBox2.Dock = System.Windows.Forms.DockStyle.FillMe.PictureBox2.Location = New System.Drawing.Point(0, 0)Me.PictureBox2.Name = "PictureBox2"Me.PictureBox2.Size = New System.Drawing.Size(1130, 751)Me.PictureBox2.TabIndex = 7Me.PictureBox2.TabStop = False''Button1'Me.Button1.Location = New System.Drawing.Point(64, 3)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(72, 43)Me.Button1.TabIndex = 9Me.Button1.Text = "播放/暫停"Me.Button1.UseVisualStyleBackColor = True''Button2'Me.Button2.Location = New System.Drawing.Point(3, 3)Me.Button2.Name = "Button2"Me.Button2.Size = New System.Drawing.Size(55, 43)Me.Button2.TabIndex = 10Me.Button2.Text = "上一首"Me.Button2.UseVisualStyleBackColor = True''Button3'Me.Button3.Location = New System.Drawing.Point(142, 3)Me.Button3.Name = "Button3"Me.Button3.Size = New System.Drawing.Size(57, 43)Me.Button3.TabIndex = 11Me.Button3.Text = "下一首"Me.Button3.UseVisualStyleBackColor = True''SplitContainer1'Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.FillMe.SplitContainer1.Location = New System.Drawing.Point(0, 0)Me.SplitContainer1.Name = "SplitContainer1"''SplitContainer1.Panel1'Me.SplitContainer1.Panel1.Controls.Add(Me.SplitContainer2)''SplitContainer1.Panel2'Me.SplitContainer1.Panel2.Controls.Add(Me.SplitContainer3)Me.SplitContainer1.Size = New System.Drawing.Size(1435, 851)Me.SplitContainer1.SplitterDistance = 301Me.SplitContainer1.TabIndex = 12''SplitContainer2'Me.SplitContainer2.Dock = System.Windows.Forms.DockStyle.FillMe.SplitContainer2.Location = New System.Drawing.Point(0, 0)Me.SplitContainer2.Name = "SplitContainer2"Me.SplitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal''SplitContainer2.Panel1'Me.SplitContainer2.Panel1.Controls.Add(Me.FlowLayoutPanel1)''SplitContainer2.Panel2'Me.SplitContainer2.Panel2.Controls.Add(Me.TextBox1)Me.SplitContainer2.Panel2.Controls.Add(Me.ListView1)Me.SplitContainer2.Size = New System.Drawing.Size(301, 851)Me.SplitContainer2.SplitterDistance = 46Me.SplitContainer2.TabIndex = 0''FlowLayoutPanel1'Me.FlowLayoutPanel1.Controls.Add(Me.Button2)Me.FlowLayoutPanel1.Controls.Add(Me.Button1)Me.FlowLayoutPanel1.Controls.Add(Me.Button3)Me.FlowLayoutPanel1.Controls.Add(Me.Button4)Me.FlowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.FillMe.FlowLayoutPanel1.Location = New System.Drawing.Point(0, 0)Me.FlowLayoutPanel1.Name = "FlowLayoutPanel1"Me.FlowLayoutPanel1.Size = New System.Drawing.Size(301, 46)Me.FlowLayoutPanel1.TabIndex = 13''Button4'Me.Button4.BackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(128, Byte), Integer))Me.Button4.Location = New System.Drawing.Point(205, 3)Me.Button4.Name = "Button4"Me.Button4.Size = New System.Drawing.Size(93, 40)Me.Button4.TabIndex = 12Me.Button4.Text = "一鍵翻譯"Me.Button4.UseVisualStyleBackColor = False''SplitContainer3'Me.SplitContainer3.Dock = System.Windows.Forms.DockStyle.FillMe.SplitContainer3.Location = New System.Drawing.Point(0, 0)Me.SplitContainer3.Name = "SplitContainer3"Me.SplitContainer3.Orientation = System.Windows.Forms.Orientation.Horizontal''SplitContainer3.Panel1'Me.SplitContainer3.Panel1.Controls.Add(Me.PictureBox1)''SplitContainer3.Panel2'Me.SplitContainer3.Panel2.Controls.Add(Me.PictureBox2)Me.SplitContainer3.Size = New System.Drawing.Size(1130, 851)Me.SplitContainer3.SplitterDistance = 96Me.SplitContainer3.TabIndex = 0''ContextMenuStrip1'Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.刪除ToolStripMenuItem})Me.ContextMenuStrip1.Name = "ContextMenuStrip1"Me.ContextMenuStrip1.Size = New System.Drawing.Size(101, 26)''刪除ToolStripMenuItem'Me.刪除ToolStripMenuItem.Name = "刪除ToolStripMenuItem"Me.刪除ToolStripMenuItem.Size = New System.Drawing.Size(100, 22)Me.刪除ToolStripMenuItem.Text = "刪除"''TextBox1'Me.TextBox1.Dock = System.Windows.Forms.DockStyle.TopMe.TextBox1.Location = New System.Drawing.Point(0, 0)Me.TextBox1.Name = "TextBox1"Me.TextBox1.Size = New System.Drawing.Size(301, 21)Me.TextBox1.TabIndex = 6''ListView1'Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1})Me.ListView1.Dock = System.Windows.Forms.DockStyle.BottomMe.ListView1.FullRowSelect = TrueMe.ListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.NoneMe.ListView1.HideSelection = FalseMe.ListView1.Location = New System.Drawing.Point(0, 27)Me.ListView1.Name = "ListView1"Me.ListView1.Size = New System.Drawing.Size(301, 774)Me.ListView1.TabIndex = 5Me.ListView1.UseCompatibleStateImageBehavior = FalseMe.ListView1.View = System.Windows.Forms.View.Details''ColumnHeader1'Me.ColumnHeader1.Width = 250''Form1'Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.FontMe.ClientSize = New System.Drawing.Size(1435, 851)Me.Controls.Add(Me.SplitContainer1)Me.Name = "Form1"Me.Text = "Form1"CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).EndInit()Me.SplitContainer1.Panel1.ResumeLayout(False)Me.SplitContainer1.Panel2.ResumeLayout(False)CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).EndInit()Me.SplitContainer1.ResumeLayout(False)Me.SplitContainer2.Panel1.ResumeLayout(False)Me.SplitContainer2.Panel2.ResumeLayout(False)Me.SplitContainer2.Panel2.PerformLayout()CType(Me.SplitContainer2, System.ComponentModel.ISupportInitialize).EndInit()Me.SplitContainer2.ResumeLayout(False)Me.FlowLayoutPanel1.ResumeLayout(False)Me.SplitContainer3.Panel1.ResumeLayout(False)Me.SplitContainer3.Panel2.ResumeLayout(False)CType(Me.SplitContainer3, System.ComponentModel.ISupportInitialize).EndInit()Me.SplitContainer3.ResumeLayout(False)Me.ContextMenuStrip1.ResumeLayout(False)Me.ResumeLayout(False)End SubFriend WithEvents PictureBox1 As PictureBoxFriend WithEvents Timer1 As TimerFriend WithEvents PictureBox2 As PictureBoxFriend WithEvents Button1 As ButtonFriend WithEvents Button2 As ButtonFriend WithEvents Button3 As ButtonFriend WithEvents SplitContainer1 As SplitContainerFriend WithEvents SplitContainer2 As SplitContainerFriend WithEvents FlowLayoutPanel1 As FlowLayoutPanelFriend WithEvents SplitContainer3 As SplitContainerFriend WithEvents Button4 As ButtonFriend WithEvents ContextMenuStrip1 As ContextMenuStripFriend WithEvents 刪除ToolStripMenuItem As ToolStripMenuItemFriend WithEvents TextBox1 As TextBoxFriend WithEvents ListView1 As ListViewFriend WithEvents ColumnHeader1 As ColumnHeader End Class?
Imports System.IO Imports System.Text.RegularExpressions Imports NAudio Imports NAudio.Wave Public Class 歌曲Private 隨機(jī)數(shù) As RandomPrivate 文件流 As WaveStreamPrivate 播放組件 As IWavePlayerPublic 路徑 As StringPublic 歌曲名 As StringPublic 歌曲類型 As StringPublic 歌詞 As New List(Of 單條歌詞)Public 歌曲長(zhǎng)度 As DoublePublic 是否翻譯 = FalsePublic Sub close()播放組件.Dispose()文件流.Dispose()End SubPublic Sub New(歌曲路徑 As String)文件流 = New MediaFoundationReader(歌曲路徑)播放組件 = New WaveOut()播放組件.Init(文件流)路徑 = 歌曲路徑歌曲名 = Path.GetFileNameWithoutExtension(路徑)歌曲類型 = Path.GetExtension(路徑)歌曲長(zhǎng)度 = TimeSpan轉(zhuǎn)秒(文件流.TotalTime)隨機(jī)數(shù) = New Random解析歌詞()End SubPrivate Sub 解析歌詞()Dim 路徑_lrc = $"{ Path.GetDirectoryName(路徑)}\{Path.GetFileNameWithoutExtension(路徑)}.lrc"If System.IO.File.Exists(路徑_lrc) = False ThenExit SubEnd IfDim STR = IO.File.ReadAllText(路徑_lrc)If InStr(STR, "[offset:0]") Then STR = Split(STR, "[offset:0]")(1)Dim arr = Split(STR, vbLf)Dim regex As Regex = New Regex("\[([0-9.:]*)\]+(.*)", RegexOptions.Compiled)For Each ss In arrDim mc As MatchCollection = regex.Matches(ss)If mc.Count = 1 ThenDim time As Double = TimeSpan.Parse("00:" & mc(0).Groups(1).Value).TotalSecondsDim word As String = mc(0).Groups(2).ValueDim SYYY = New 單條歌詞(time, Color.FromArgb(隨機(jī)數(shù).Next(200, 255), 隨機(jī)數(shù).Next(200, 255), 隨機(jī)數(shù).Next(200, 255)), word)If word <> "" Then歌詞.Add(SYYY)End IfEnd IfNextEnd SubPublic Structure 單條歌詞Public 歌詞文本 As StringPublic 歌詞時(shí)間 As DoublePublic 顏色 As ColorSub New(timeS, COLORS, WORDS)歌詞文本 = WORDS顏色 = COLORS歌詞時(shí)間 = timeSEnd SubEnd StructurePublic Sub 翻譯英文()If 是否翻譯 = True ThenReturnEnd IfIf 歌詞.Count <= 1 ThenReturnEnd IfDim S As New List(Of String)Dim T As New List(Of Double)Dim C As New List(Of Color)For Each I In 歌詞S.Add(I.歌詞文本)T.Add(I.歌詞時(shí)間)C.Add(I.顏色)Next歌詞.Clear()Dim ARR = Split(百度翻譯(Join(S.ToArray, vbCrLf)), vbCrLf)For i = 0 To S.Count - 1歌詞.Add(New 單條歌詞(T(i), C(i), S(i) & vbCrLf & ARR(i + 1)))Next是否翻譯 = TrueEnd SubPublic Sub 播放音樂(lè)()播放組件.Play()End SubPublic Sub 暫停音樂(lè)()播放組件.Pause()End SubPublic Sub 停止音樂(lè)()播放組件.Stop()當(dāng)前播放時(shí)間_百分比 = 0End SubPublic Property 當(dāng)前播放時(shí)間_百分比() As DoubleGetReturn 當(dāng)前播放時(shí)間 / 歌曲長(zhǎng)度End GetSet(ByVal value As Double)文件流.Position = value * 文件流.LengthEnd SetEnd PropertyPrivate Shared Function TimeSpan轉(zhuǎn)秒(T As TimeSpan) As DoubleReturn T.Minutes * 60 + T.Seconds + T.Milliseconds / 1000End FunctionPublic Property 當(dāng)前播放時(shí)間() As DoubleGetReturn TimeSpan轉(zhuǎn)秒(文件流.CurrentTime)End GetSet(ByVal value As Double)文件流.Position = (value / 歌曲長(zhǎng)度) * 文件流.LengthEnd SetEnd PropertyPublic Property 播放狀態(tài)() As 播放GetIf 播放組件.PlaybackState = PlaybackState.Playing Then Return 播放.播放If 播放組件.PlaybackState = PlaybackState.Paused Then Return 播放.暫停If 播放組件.PlaybackState = PlaybackState.Stopped Then Return 播放.停止End GetSet(ByVal value As 播放)If value = 播放.播放 Then 播放音樂(lè)()If value = 播放.暫停 Then 暫停音樂(lè)()If value = 播放.停止 Then 停止音樂(lè)()End SetEnd PropertyPublic Enum 播放停止 = 0播放 = 1暫停 = 2End EnumEnd Class?
Imports System.IO Imports System.Net Imports System.Security.Cryptography Imports System.Text Imports System.Text.RegularExpressionsPublic Module 翻譯Public Function GetData(ByVal url As String, ByVal data As String) As StringDim request As HttpWebRequest = WebRequest.Create(url + "?" + data)request.Method = "GET"Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)Return sr.ReadToEndEnd FunctionPublic Function PostData(ByVal url As String, ByVal data As String) As StringServicePointManager.Expect100Continue = FalseDim request As HttpWebRequest = WebRequest.Create(url)'//Post請(qǐng)求方式request.Method = "POST"'內(nèi)容類型request.ContentType = "application/x-www-form-urlencoded"'將URL編碼后的字符串轉(zhuǎn)化為字節(jié)Dim encoding As New UTF8Encoding()Dim bys As Byte() = encoding.GetBytes(data)'設(shè)置請(qǐng)求的 ContentLength request.ContentLength = bys.Length'獲得請(qǐng) 求流Dim newStream As Stream = request.GetRequestStream()newStream.Write(bys, 0, bys.Length)newStream.Close()'獲得響應(yīng)流Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)Return sr.ReadToEndEnd FunctionPublic Function MD5(ByVal StrSource As String, ByVal Code As Integer) As StringDim str As String = ""Dim md5Hasher As New MD5CryptoServiceProvider()Dim data As Byte() = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(StrSource))Dim sBuilder As New StringBuilder()Dim i As IntegerFor i = 0 To data.Length - 1sBuilder.Append(data(i).ToString("x2"))Next iSelect Case CodeCase 16str = sBuilder.ToString().Substring(0, 16)Case 32str = sBuilder.ToString().Substring(0, 32)End SelectReturn strEnd FunctionPublic Function utostring(s As String)If InStr(s, "\u") = False ThenReturn sEnd IfDim ret = ""For i = 0 To (s.Count / 6) - 1ret += ChrW(Convert.ToInt32(Mid(s, i * 6 + 3, 4), 16))NextReturn retEnd FunctionPublic Function 百度翻譯(str As String)' Threading.Thread.Sleep(2000)Dim sign = MD5(APPID + str + "0" + KEY, 32)Dim s = $"q={str}&from=auto&to=auto&appid={APPID}&salt=0&sign={sign}"Dim ret = PostData("https://api.fanyi.baidu.com/api/trans/vip/translate", s)Debug.Print(ret)Dim arr = Split(ret, "dst"":""")Dim STRS = ""For Each I In arrSTRS += Split(I, """}")(0) & vbCrLfNextTryReturn utostring(STRS)Catch ex As ExceptionReturn STRSEnd TryEnd Function'將unicode轉(zhuǎn)成中文,如:ZRO \u8033\u9EA6 12345,轉(zhuǎn)后為:ZRO 耳麥 12345Public Function UnicodeToString(strCode As String) As StringUnicodeToString = strCodeIf InStr(UnicodeToString, "\u") <= 0 ThenExit FunctionEnd IfstrCode = LCase(strCode)Dim mc As MatchCollectionmc = Regex.Matches(strCode, "\\u\S{1,4}")For Each m In mcstrCode = Replace(strCode, m.ToString, ChrW("&H" & Mid(CStr(m.ToString), 3, 6)))NextstrCode = Strings.Replace(strCode, "\r", vbCrLf)UnicodeToString = strCodeEnd FunctionPublic APPID = "百度翻譯APIid"Public KEY = "百度翻譯API key" End Module?
總結(jié)
以上是生活随笔為你收集整理的VB.net写的音乐播放器,带百度翻译歌词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java WebSocket编程与网页简
- 下一篇: 常见的一些代码编辑器