Sunday, June 12, 2011

Audio Player Using AxWindowsMediaPlayer in VB.Net



Public Class Audio_Player
#Region "All Controls"
    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Try
            Call GetOpenMedia()
            TrackBar1.Maximum = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration
            Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString
            Timer1.Interval = 100
            Timer1.Enabled = True
        Catch ex As Exception

        End Try

    End Sub

    Private Sub OpenToolStripMenuItem_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Try
            Call GetOpenMedia()
            TrackBar1.Maximum = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration
            Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString
            Timer1.Interval = 100
            Timer1.Enabled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Try
            AxWindowsMediaPlayer1.Ctlcontrols.play()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnPause_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPause.Click
        Try
            AxWindowsMediaPlayer1.Ctlcontrols.pause()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Try
            Label1.Text = "00:00"
            AxWindowsMediaPlayer1.Ctlcontrols.stop()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub GetOpenMedia()
        Play_List.DataGridView1.Rows.Clear()
        Dim OpenMedia As New OpenFileDialog
        OpenMedia.InitialDirectory = System.Environment.SpecialFolder.CommonMusic
        OpenMedia.FileName = ""
        OpenMedia.Multiselect = False
        OpenMedia.Filter = "Audio Files (*.wav;*.snd;*.au;*.aif;*.aifc;*.aiff;*.wma;*.mp2;*.mp3;*.adts;*.adt;*.acc)|*.wav;*.snd;*.au;*.aif;*.aifc;*.aiff;*.wma;*.mp2;*.mp3;*.adts;*.adt;*.acc"
        If OpenMedia.ShowDialog() = DialogResult.OK Then
            Play_List.DataGridView1.Rows.Add(OpenMedia.FileName)
            AxWindowsMediaPlayer1.URL = OpenMedia.FileName
        End If
    End Sub

    Private Sub OpenFilesToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenFilesToolStripMenuItem.Click
        Try

            Dim OpenMedia As New OpenFileDialog
            OpenMedia.InitialDirectory = System.Environment.SpecialFolder.CommonMusic
            OpenMedia.FileName = ""
            OpenMedia.Multiselect = True
            OpenMedia.Filter = "Audio Files (*.wav;*.snd;*.au;*.aif;*.aifc;*.aiff;*.wma;*.mp2;*.mp3;*.adts;*.adt;*.acc)|*.wav;*.snd;*.au;*.aif;*.aifc;*.aiff;*.wma;*.mp2;*.mp3;*.adts;*.adt;*.acc"
            If OpenMedia.ShowDialog() = DialogResult.OK Then
                'For Each str1 As String In OpenMedia.FileNames
                '    Play_List.ListBox1.Items.Add(str1)
                'Next
                For Each Str As String In OpenMedia.FileNames
                    Play_List.DataGridView1.Rows.Add(Str)
                Next
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Audio_Player_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        tmrFR.Interval = 100
        TrackBar2.Value = 50
        ProgressBar1.Value = TrackBar2.Value
        AxWindowsMediaPlayer1.settings.volume = TrackBar2.Value
        Label3.Text = "Volume: " & TrackBar2.Value & " %"
        AxWindowsMediaPlayer1.Visible = False
        Timer1.Interval = 100
        If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsPlaying Then
            Timer1.Enabled = True
        Else
            Timer1.Enabled = False
        End If
        Play_List.Visible = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            TrackBar1.Maximum = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration
            Label1.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
            TrackBar1.Value = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
            Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString

        Catch ex As Exception

        End Try
    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        Try
            AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = TrackBar1.Value
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub TrackBar2_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        Try
            AxWindowsMediaPlayer1.settings.volume = TrackBar2.Value
            ProgressBar1.Value = TrackBar2.Value
            Label3.Text = "Volume: " & TrackBar2.Value & " %"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnMuteOnOff_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMuteOnOff.Click
        Try
            If AxWindowsMediaPlayer1.settings.mute = False Then
                btnMuteOnOff.FlatStyle = FlatStyle.Flat
                AxWindowsMediaPlayer1.settings.mute = True
            Else
                AxWindowsMediaPlayer1.settings.mute = False
                btnMuteOnOff.FlatStyle = FlatStyle.Standard
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub PlayListToolStripMenuItem_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PlayListToolStripMenuItem.Click
        If Play_List.Visible = False Then
            Play_List.Visible = True
        Else
            Play_List.Visible = False
        End If
    End Sub

    Private Sub btnFastForword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFastForword.Click
        Try
            AxWindowsMediaPlayer1.Ctlcontrols.fastForward()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnFastReverse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFastReverse.Click
        Try
            tmrFR.Enabled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub tmrFR_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrFR.Tick
        Try
            If Not AxWindowsMediaPlayer1.Ctlcontrols.currentPosition <= 0 Then
                AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition - 1
                If AxWindowsMediaPlayer1.Ctlcontrols.currentPosition <= 0 Then
                    tmrFR.Enabled = False
                    AxWindowsMediaPlayer1.Ctlcontrols.stop()
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub
#End Region

End Class




1 comment: