Public Class Form1
    Dim paths As String()
    Dim filenames As String()
    Dim MyRandom As Integer
    Private Const SW_SHOWNORMAL = 1
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
      (ByVal hwnd As Integer, ByVal lpOperation As String, _
      ByVal lpFile As String, ByVal lpParameters As String, _
      ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button7.Text = "Отк."
    End Sub

    Private Sub ListBox1_DragDrop(sender As Object, e As DragEventArgs) Handles ListBox1.DragDrop
        Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
        For Each FileName As String In Files
            ListBox1.Items.Add(FileName)
        Next
    End Sub

    Private Sub ListBox1_DragEnter(sender As Object, e As DragEventArgs) Handles ListBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    Private Sub ListBox1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDoubleClick
        ' Двойной щелчёк в плейлисте окна
        Dim start1 As Integer = 0
        If start1 = 1 Then
            If AxWindowsMediaPlayer1.URL = ListBox1.Text Then
                AxWindowsMediaPlayer1.Ctlcontrols.play()

            End If
        End If
        If start1 = 0 Then
            AxWindowsMediaPlayer1.URL = ListBox1.Text
            start1 = +1
            AxWindowsMediaPlayer1.Ctlcontrols.play()
            Timer1.Start()
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            AxWindowsMediaPlayer1.URL = paths(ListBox1.SelectedIndex)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        Dim FoldD As New FolderBrowserDialog
        If FoldD.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.AddRange(System.IO.Directory.GetFiles(FoldD.SelectedPath, "*"))
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        OpenFileDialog1.Filter = "Mp3 файлы (*.mp3)|*.mp3|Все файлы (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.Cancel Then Exit Sub
        AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
        ListBox1.Items.Add(OpenFileDialog1.FileName)
        Timer1.Start()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' Открыть плейлист
        ListBox1.Items.Clear()
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\playlist.txt") Then
            Dim reader As New IO.StreamReader(Application.StartupPath & "\playlist.txt")
            Try
                Do
                    ListBox1.Items.Add(reader.ReadLine)

                Loop
            Catch ex As Exception
                reader.Close()
                ListBox1.SelectedIndex = 0
                If My.Settings.AutoPlay = True Then
                    AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
                    Timer1.Start()
                    My.Settings.Repeat = True
                End If
            End Try
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ' Сохранить плейлист
        Dim count As Integer = -1
        Dim writer As New IO.StreamWriter(Application.StartupPath & "\playlist.txt")
        Dim current As Integer = ListBox1.SelectedIndex
        Try
            Do Until count = ListBox1.Items.Count
                ListBox1.SelectedIndex = count + 1
                writer.WriteLine(ListBox1.SelectedItem)
                count = count + 1
            Loop
        Catch ex As Exception
            writer.Close()
            ListBox1.SelectedIndex = current
            MsgBox("Плейлист Сохранен")
        End Try
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        ' Удалить трек из плейлиста
        Dim newItem As String = ListBox1.SelectedItem
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\playlist.txt") Then
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End If
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        ' Очистить окно плейлиста
        ListBox1.Items.Clear()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        ' Скрыть, открыть медиа плеер
        If Button7.Text = "Вкл." Then
            AxWindowsMediaPlayer1.uiMode = "Full"
            Button7.Text = "Отк."
        Else
            AxWindowsMediaPlayer1.uiMode = "None"
            Button7.Text = "Вкл."
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Try

        Catch ex As Exception

        End Try

        Try

        Catch ex As Exception
            
        End Try

        Dim itemno As Integer
        itemno = ListBox1.SelectedIndex
        Try
            If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
                Me.ListBox1.SelectedIndex = itemno + 1
                AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
                AxWindowsMediaPlayer1.Ctlcontrols.play()
                Timer1.Start()
            Else
                itemno = 0
            End If
        Catch ex As Exception
        End Try

        MyRandom = Rnd() * 9
        'MyRandom.ToString()

        If MyRandom = 0 Then
            Label5.ForeColor = Color.Red
        End If
        If MyRandom = 1 Then
            Label5.ForeColor = Color.Green
        End If
        If MyRandom = 2 Then
            Label5.ForeColor = Color.Blue
        End If
        If MyRandom = 3 Then
            Label5.ForeColor = Color.Yellow
        End If
        If MyRandom = 4 Then
            Label5.ForeColor = Color.DarkRed
        End If
        If MyRandom = 5 Then
            Label5.ForeColor = Color.Navy
        End If
        If MyRandom = 6 Then
            Label5.ForeColor = Color.Olive
        End If
        If MyRandom = 7 Then
            Label5.ForeColor = Color.Black
        End If
        If MyRandom = 8 Then
            Label5.ForeColor = Color.Teal
        End If
        If MyRandom = 9 Then
            Label5.ForeColor = Color.Lime
        End If

        'Отчёт Времени
        TextBox4.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString

        ' Длительность песни  
        Try
            TextBox3.Text = AxWindowsMediaPlayer1.Ctlcontrols().currentItem.durationString
        Catch ex As Exception
        End Try

        'Воспроизведение, название и скорость воспроизведения
        TextBox2.Text = AxWindowsMediaPlayer1.status

        'Название песни
        Try
            TextBox5.Text = AxWindowsMediaPlayer1.currentMedia.name
        Catch ex As Exception
        End Try

        'Имя артиста
        Try
            TextBox6.Text = AxWindowsMediaPlayer1.currentMedia.getItemInfo("Artist")
        Catch ex As Exception
        End Try

        'Название альбома
        Try
            TextBox7.Text = AxWindowsMediaPlayer1.currentMedia.getItemInfo("Album")
        Catch ex As Exception
        End Try

        'Скорость воспроизведения
        Try
            TextBox8.Text = AxWindowsMediaPlayer1.currentMedia.getItemInfo("Bitrate") / 1000 & " кбит/с"
        Catch ex As Exception
        End Try

        ' Год выпуска альбома
        Try
            TextBox10.Text = AxWindowsMediaPlayer1.currentMedia.getItemInfo("WM/Year")
        Catch ex As Exception
        End Try

        'Жанр исполняемой песни
        Try
            TextBox9.Text = AxWindowsMediaPlayer1.currentMedia.getItemInfo("Genre")
        Catch ex As Exception
        End Try
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub СправкаToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles СправкаToolStripMenuItem.Click
        Form2.Show()
    End Sub

    Private Sub ПлеерWindowsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ПлеерWindowsToolStripMenuItem.Click
        ShellExecute(0, "open", ListBox1.Text, 0, 0, SW_SHOWNORMAL)
    End Sub

    Private Sub ОткрытьПапкуToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ОткрытьПапкуToolStripMenuItem.Click
        ListBox1.Items.Clear()
        Dim FoldD As New FolderBrowserDialog
        If FoldD.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.AddRange(System.IO.Directory.GetFiles(FoldD.SelectedPath, "*"))
        End If
    End Sub

    Private Sub ДобавитьМр3ФайлToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ДобавитьМр3ФайлToolStripMenuItem.Click
        OpenFileDialog1.Filter = "Mp3 файлы (*.mp3)|*.mp3|Все файлы (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.Cancel Then Exit Sub
        AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
        ListBox1.Items.Add(OpenFileDialog1.FileName)
        Timer1.Start()
    End Sub

    Private Sub ОткрытьПлейлистToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ОткрытьПлейлистToolStripMenuItem.Click
        ' Открыть плейлист
        ListBox1.Items.Clear()
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\playlist.txt") Then
            Dim reader As New IO.StreamReader(Application.StartupPath & "\playlist.txt")
            Try
                Do
                    ListBox1.Items.Add(reader.ReadLine)

                Loop
            Catch ex As Exception
                reader.Close()
                ListBox1.SelectedIndex = 0
                If My.Settings.AutoPlay = True Then
                    AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
                    Timer1.Start()
                    My.Settings.Repeat = True
                End If
            End Try
        End If
    End Sub

    Private Sub СохранитьПлейлистToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles СохранитьПлейлистToolStripMenuItem.Click
        ' Сохранить плейлист
        Dim count As Integer = -1
        Dim writer As New IO.StreamWriter(Application.StartupPath & "\playlist.txt")
        Dim current As Integer = ListBox1.SelectedIndex
        Try
            Do Until count = ListBox1.Items.Count
                ListBox1.SelectedIndex = count + 1
                writer.WriteLine(ListBox1.SelectedItem)
                count = count + 1
            Loop
        Catch ex As Exception
            writer.Close()
            ListBox1.SelectedIndex = current
            MsgBox("Плейлист Сохранен")
        End Try
    End Sub

    Private Sub УдалитьТрекToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles УдалитьТрекToolStripMenuItem.Click
        Dim newItem As String = ListBox1.SelectedItem
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\playlist.txt") Then
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End If
    End Sub

    Private Sub ВыходToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ВыходToolStripMenuItem.Click
        End
    End Sub

    Private Sub ПлеерРедактораToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ПлеерРедактораToolStripMenuItem.Click
        If AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem Then
            AxWindowsMediaPlayer1.Ctlcontrols.play()
            Timer1.Start()
        End If
    End Sub
End Class
