VB.net写的音乐播放器,带百度翻译歌词

 

 

Imports System.Drawing.Text
Imports System.IO

Public Class Form1
    Public RND As New Random
    Public g As BufferedGraphics
    Public g2 As BufferedGraphics
    Public fonts20 As New Font("微软雅黑", 20)
    Public fonts11 As New Font("宋体", 9)
    Public 居中画字样式 As StringFormat
    Public 播放器 As 歌曲






    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load



        g = BufferedGraphicsManager.Current.Allocate(PictureBox1.CreateGraphics, PictureBox1.DisplayRectangle)
        g2 = BufferedGraphicsManager.Current.Allocate(PictureBox2.CreateGraphics, PictureBox2.DisplayRectangle)
        读取音乐文件并且刷新listview1()
        居中画字样式 = New StringFormat()
        居中画字样式.LineAlignment = StringAlignment.Center
        居中画字样式.Alignment = StringAlignment.Center
    End Sub

    Public gequ As New Generic.Dictionary(Of String, String)
    Public Sub 读取音乐文件并且刷新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 files
            If Path.GetExtension(file) = ".mp3" Then
                Dim 歌曲名 = Path.GetFileNameWithoutExtension(file)
                gequ.Add(file, 歌曲名)
                Dim t As ListViewItem = New ListViewItem({歌曲名})
                t.Tag = file
                t.BackColor = Color.FromArgb(RND.Next(200, 255), RND.Next(200, 255), RND.Next(200, 255))
                ListView1.Items.Add(t)
            End If
        Next
        ListView1.EndUpdate()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If 播放器 IsNot Nothing Then
            g.Graphics.Clear(Color.Black)
            g.Graphics.FillRectangle(Brushes.Green, New RectangleF(0, 0, 播放器.当前播放时间_百分比 * PictureBox1.Width, PictureBox1.Height))
            '绘制时间
            g.Graphics.DrawString(播放器.当前播放时间 & "/" & 播放器.歌曲长度, fonts20, Brushes.Gold, 0, 0)
            '绘制歌词
            For I = 1 To 播放器.歌词.Count - 2
                If 播放器.歌词(I + 1).歌词时间 > 播放器.当前播放时间 Then
                    g.Graphics.DrawString(播放器.歌词(I).歌词文本, fonts20, Brushes.Gold, New Rectangle(Point.Empty, PictureBox1.Size), 居中画字样式)
                    Exit For
                End If
            Next
            g.Render()
            '绘制歌词
            g2.Graphics.Clear(Color.Black)
            Dim 当前歌词 As Boolean = False
            For I = 0 To 播放器.歌词.Count - 1
                If I <> 播放器.歌词.Count - 1 AndAlso 播放器.歌词(I + 1).歌词时间 > 播放器.当前播放时间 And 当前歌词 = False Then
                    Dim se = 播放器.歌词(I).颜色
                    Dim se2 = Color.FromArgb(se.R - 100, se.G - 100, se.B - 100)
                    Dim 当前歌词长度 = 播放器.歌词(I + 1).歌词时间 - 播放器.歌词(I).歌词时间
                    Dim 这句歌词现在播放到 = 播放器.当前播放时间 - 播放器.歌词(I).歌词时间
                    Dim kd = (PictureBox2.Width / 3) / 50
                    For N = 0 To (50 * (这句歌词现在播放到 / 当前歌词长度))
                        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) * 当前绘制高度, kd - 2, 当前绘制高度))
                    Next
                    g2.Graphics.DrawString(播放器.歌词(I).歌词文本, fonts11, Brushes.White, New Rectangle((I Mod 3) * (PictureBox2.Width / 3), Int(I / 3) * 当前绘制高度, (PictureBox2.Width / 3), 当前绘制高度), 居中画字样式)
                    当前歌词 = True
                Else
                    g2.Graphics.FillRectangle(New SolidBrush(播放器.歌词(I).颜色), New Rectangle((I Mod 3) * (PictureBox2.Width / 3), Int(I / 3) * 当前绘制高度, (PictureBox2.Width / 3), 当前绘制高度))
                    g2.Graphics.DrawString(播放器.歌词(I).歌词文本, fonts11, Brushes.Black, New Rectangle((I Mod 3) * (PictureBox2.Width / 3), Int(I / 3) * 当前绘制高度, (PictureBox2.Width / 3), 当前绘制高度), 居中画字样式)
                End If
            Next
            g2.Render()
        End If
    End Sub
    Public 当前绘制高度 As Integer = 35
    Private Sub 进度条弹起(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
        If 播放器 IsNot Nothing Then
            播放器.当前播放时间_百分比 = e.X / PictureBox1.Width
            播放器.播放状态 = 歌曲.播放.播放
        End If
    End Sub

    Private Sub 歌词弹起(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUp
        Dim X As Integer = Int(e.X / (PictureBox2.Width / 3))
        Dim Y As Integer = Int(e.Y / 当前绘制高度)
        Dim INDEX = X + Y * 3
        If INDEX < 播放器.歌词.Count Then
            播放器.当前播放时间 = 播放器.歌词(INDEX).歌词时间
            播放器.播放音乐()
        End If
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.SelectedItems.Count >= 1 Then
            If 播放器 IsNot Nothing Then
                播放器.close()
                播放器 = Nothing
            End If
            播放器 = New 歌曲(ListView1.SelectedItems(0).Tag)
            播放器.播放音乐()
            当前绘制高度 = PictureBox2.Height / (播放器.歌词.Count / 3 + 1)
        End If
    End Sub
    Private Sub 上一首(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            If ListView1.SelectedItems.Count >= 1 Then
                If ListView1.SelectedItems(0).Index >= 1 Then
                    Dim ID = ListView1.SelectedItems(0).Index - 1
                    ListView1.SelectedItems.Clear()
                    ListView1.Items(ID).Selected = True
                    ListView1.Focus()
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub 下一首(sender As Object, e As EventArgs) Handles Button3.Click
        'Try
        If ListView1.SelectedItems.Count >= 1 Then
            If ListView1.SelectedItems(0).Index < ListView1.Items.Count - 1 Then
                Dim ID = ListView1.SelectedItems(0).Index + 1
                ListView1.SelectedItems.Clear()
                ListView1.Items(ID).Selected = True
                ListView1.Focus()
            End If
        End If        '  Catch ex As Exception
        ' End Try
    End Sub

    Private Sub 播放or暂停(sender As Object, e As EventArgs) Handles Button1.Click
        Try

            If 播放器.播放状态 = 歌曲.播放.停止 Or 播放器.播放状态 = 歌曲.播放.暂停 Then
                Button1.Text = "暂停"
                播放器.播放状态 = 歌曲.播放.播放
            ElseIf 播放器.播放状态 = 歌曲.播放.播放 Then
                Button1.Text = "播放"
                播放器.播放状态 = 歌曲.播放.暂停

            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click

    End Sub

    Private Sub SplitContainer3_Panel2_Paint(sender As Object, e As PaintEventArgs) Handles SplitContainer3.Paint
        g = BufferedGraphicsManager.Current.Allocate(PictureBox1.CreateGraphics, PictureBox1.DisplayRectangle)
        g2 = BufferedGraphicsManager.Current.Allocate(PictureBox2.CreateGraphics, PictureBox2.DisplayRectangle)
        If 播放器 IsNot Nothing Then
            当前绘制高度 = PictureBox2.Height / (播放器.歌词.Count / 3 + 1)
        End If
        ListView1.Columns(0).Width = ListView1.Width
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If 播放器 IsNot Nothing Then
            播放器.翻译英文()
        End If
        MsgBox("翻译成功")
    End Sub

    Private Sub ListView1_MouseUp(sender As Object, e As MouseEventArgs) Handles ListView1.MouseUp
        If e.Button = MouseButtons.Right Then
            ContextMenuStrip1.Show(Cursor.Position)
        End If
    End Sub

    Private Sub 删除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 删除ToolStripMenuItem.Click
        If ListView1.SelectedItems.Count >= 1 Then
            Dim FILE = ListView1.SelectedItems(0).Tag
            ListView1.SelectedItems(0).Remove()
            IO.File.Delete(FILE)
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        ListView1.BeginUpdate()

        If TextBox1.Text = "" Then
            ListView1.Items.Clear()
            For Each I In gequ

                Dim 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.Key
                ListView1.Items.Add(T)
            Next
        Else
            ListView1.Items.Clear()
            For Each I In gequ
                If InStr(I.Value, TextBox1.Text) Then
                    Dim 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.Key
                    ListView1.Items.Add(T)
                End If

            Next
        End If
        ListView1.EndUpdate()
    End Sub
End Class


 

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form 重写 Dispose,以清理组件列表。
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改它。  
    '不要使用代码编辑器修改它。
    <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.Fill
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(1130, 96)
        Me.PictureBox1.TabIndex = 6
        Me.PictureBox1.TabStop = False
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 10
        '
        'PictureBox2
        '
        Me.PictureBox2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.PictureBox2.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(1130, 751)
        Me.PictureBox2.TabIndex = 7
        Me.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 = 9
        Me.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 = 10
        Me.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 = 11
        Me.Button3.Text = "下一首"
        Me.Button3.UseVisualStyleBackColor = True
        '
        'SplitContainer1
        '
        Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.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 = 301
        Me.SplitContainer1.TabIndex = 12
        '
        'SplitContainer2
        '
        Me.SplitContainer2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.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 = 46
        Me.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.Fill
        Me.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 = 12
        Me.Button4.Text = "一键翻译"
        Me.Button4.UseVisualStyleBackColor = False
        '
        'SplitContainer3
        '
        Me.SplitContainer3.Dock = System.Windows.Forms.DockStyle.Fill
        Me.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 = 96
        Me.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.Top
        Me.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.Bottom
        Me.ListView1.FullRowSelect = True
        Me.ListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None
        Me.ListView1.HideSelection = False
        Me.ListView1.Location = New System.Drawing.Point(0, 27)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(301, 774)
        Me.ListView1.TabIndex = 5
        Me.ListView1.UseCompatibleStateImageBehavior = False
        Me.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.Font
        Me.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 Sub
    Friend WithEvents PictureBox1 As PictureBox
    Friend WithEvents Timer1 As Timer
    Friend WithEvents PictureBox2 As PictureBox
    Friend WithEvents Button1 As Button
    Friend WithEvents Button2 As Button
    Friend WithEvents Button3 As Button
    Friend WithEvents SplitContainer1 As SplitContainer
    Friend WithEvents SplitContainer2 As SplitContainer
    Friend WithEvents FlowLayoutPanel1 As FlowLayoutPanel
    Friend WithEvents SplitContainer3 As SplitContainer
    Friend WithEvents Button4 As Button
    Friend WithEvents ContextMenuStrip1 As ContextMenuStrip
    Friend WithEvents 删除ToolStripMenuItem As ToolStripMenuItem
    Friend WithEvents TextBox1 As TextBox
    Friend WithEvents ListView1 As ListView
    Friend WithEvents ColumnHeader1 As ColumnHeader
End Class

 

Imports System.IO
Imports System.Text.RegularExpressions
Imports NAudio
Imports NAudio.Wave
Public Class 歌曲
    Private 随机数 As Random
    Private 文件流 As WaveStream
    Private 播放组件 As IWavePlayer
    Public 路径 As String
    Public 歌曲名 As String
    Public 歌曲类型 As String
    Public 歌词 As New List(Of 单条歌词)
    Public 歌曲长度 As Double
    Public 是否翻译 = False
    Public Sub close()
        播放组件.Dispose()
        文件流.Dispose()
    End Sub

    Public Sub New(歌曲路径 As String)
        文件流 = New MediaFoundationReader(歌曲路径)
        播放组件 = New WaveOut()
        播放组件.Init(文件流)
        路径 = 歌曲路径
        歌曲名 = Path.GetFileNameWithoutExtension(路径)
        歌曲类型 = Path.GetExtension(路径)
        歌曲长度 = TimeSpan转秒(文件流.TotalTime)
        随机数 = New Random
        解析歌词()
    End Sub
    Private Sub 解析歌词()
        Dim 路径_lrc = $"{ Path.GetDirectoryName(路径)}\{Path.GetFileNameWithoutExtension(路径)}.lrc"
        If System.IO.File.Exists(路径_lrc) = False Then
            Exit Sub
        End If
        Dim 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 arr
            Dim mc As MatchCollection = regex.Matches(ss)
            If mc.Count = 1 Then
                Dim time As Double = TimeSpan.Parse("00:" & mc(0).Groups(1).Value).TotalSeconds
                Dim word As String = mc(0).Groups(2).Value
                Dim SYYY = New 单条歌词(time, Color.FromArgb(随机数.Next(200, 255), 随机数.Next(200, 255), 随机数.Next(200, 255)), word)
                If word <> "" Then
                    歌词.Add(SYYY)
                End If

            End If
        Next
    End Sub
    Public Structure 单条歌词
        Public 歌词文本 As String
        Public 歌词时间 As Double
        Public 颜色 As Color
        Sub New(timeS, COLORS, WORDS)
            歌词文本 = WORDS
            颜色 = COLORS
            歌词时间 = timeS
        End Sub



    End Structure

    Public Sub 翻译英文()
        If 是否翻译 = True Then
            Return
        End If
        If 歌词.Count <= 1 Then
            Return
        End If
        Dim 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.歌词时间)
            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
        是否翻译 = True
    End Sub
    Public Sub 播放音乐()
        播放组件.Play()
    End Sub
    Public Sub 暂停音乐()
        播放组件.Pause()
    End Sub
    Public Sub 停止音乐()
        播放组件.Stop()
        当前播放时间_百分比 = 0
    End Sub
    Public Property 当前播放时间_百分比() As Double
        Get
            Return 当前播放时间 / 歌曲长度
        End Get
        Set(ByVal value As Double)
            文件流.Position = value * 文件流.Length
        End Set
    End Property
    Private Shared Function TimeSpan转秒(T As TimeSpan) As Double
        Return T.Minutes * 60 + T.Seconds + T.Milliseconds / 1000
    End Function
    Public Property 当前播放时间() As Double
        Get
            Return TimeSpan转秒(文件流.CurrentTime)
        End Get
        Set(ByVal value As Double)
            文件流.Position = (value / 歌曲长度) * 文件流.Length
        End Set
    End Property
    Public Property 播放状态() As 播放
        Get
            If 播放组件.PlaybackState = PlaybackState.Playing Then Return 播放.播放
            If 播放组件.PlaybackState = PlaybackState.Paused Then Return 播放.暂停
            If 播放组件.PlaybackState = PlaybackState.Stopped Then Return 播放.停止
        End Get
        Set(ByVal value As 播放)
            If value = 播放.播放 Then 播放音乐()
            If value = 播放.暂停 Then 暂停音乐()
            If value = 播放.停止 Then 停止音乐()
        End Set
    End Property

    Public Enum 播放
        停止 = 0
        播放 = 1
        暂停 = 2
    End Enum



End Class

 

Imports System.IO
Imports System.Net
Imports System.Security.Cryptography
Imports System.Text
Imports System.Text.RegularExpressions

Public Module 翻译
    Public Function GetData(ByVal url As String, ByVal data As String) As String

        Dim request As HttpWebRequest = WebRequest.Create(url + "?" + data)
        request.Method = "GET"
        Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)
        Return sr.ReadToEnd
    End Function

    Public Function PostData(ByVal url As String, ByVal data As String) As String

        ServicePointManager.Expect100Continue = False
        Dim request As HttpWebRequest = WebRequest.Create(url)
        '//Post请求方式
        request.Method = "POST"

        '内容类型
        request.ContentType = "application/x-www-form-urlencoded"
        '将URL编码后的字符串转化为字节
        Dim encoding As New UTF8Encoding()
        Dim bys As Byte() = encoding.GetBytes(data)
        '设置请求的 ContentLength 
        request.ContentLength = bys.Length
        '获得请 求流
        Dim newStream As Stream = request.GetRequestStream()
        newStream.Write(bys, 0, bys.Length)
        newStream.Close()
        '获得响应流
        Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)
        Return sr.ReadToEnd
    End Function
    Public Function MD5(ByVal StrSource As String, ByVal Code As Integer) As String
        Dim 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 Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
        Select Case Code
            Case 16
                str = sBuilder.ToString().Substring(0, 16)
            Case 32
                str = sBuilder.ToString().Substring(0, 32)
        End Select
        Return str
    End Function
    Public Function utostring(s As String)
        If InStr(s, "\u") = False Then
            Return s
        End If
        Dim ret = ""
        For i = 0 To (s.Count / 6) - 1
            ret += ChrW(Convert.ToInt32(Mid(s, i * 6 + 3, 4), 16))
        Next
        Return ret
    End Function

    Public 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 arr
            STRS += Split(I, """}")(0) & vbCrLf
        Next

        Try
            Return utostring(STRS)
        Catch ex As Exception
            Return STRS
        End Try


    End Function
    '将unicode转成中文,如:ZRO \u8033\u9EA6 12345,转后为:ZRO 耳麦 12345
    Public Function UnicodeToString(strCode As String) As String
        UnicodeToString = strCode
        If InStr(UnicodeToString, "\u") <= 0 Then
            Exit Function
        End If
        strCode = LCase(strCode)
        Dim mc As MatchCollection
        mc = Regex.Matches(strCode, "\\u\S{1,4}")
        For Each m In mc
            strCode = Replace(strCode, m.ToString, ChrW("&H" & Mid(CStr(m.ToString), 3, 6)))
        Next
        strCode = Strings.Replace(strCode, "\r", vbCrLf)
        UnicodeToString = strCode
    End Function

    Public APPID = "百度翻译APIid"
    Public KEY = "百度翻译API key"
End Module

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值