Auto-play next songs correct code...

fortunatekidz

New member
Joined
Mar 2, 2018
Messages
4
Programming Experience
Beginner
Hi , coding master in visual basic studio. I have a project in building mp3 player, It almost done, but there is one problem which is the media player won't auto-play next song. I tried find it online but they don't work in my project. Hope all masters here could help me...Thanks alot
Untitle4d.jpg

VB.NET:
Imports AxWMPLib

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem.ToString
        currentTrack = ListBox1.SelectedIndex
    End Sub

    Private Sub AxWindowsMediaPlayer1_Enter(sender As Object, e As EventArgs) Handles AxWindowsMediaPlayer1.Enter
        If (AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped) Then
            Timer1.Enabled = True
            AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
            AxWindowsMediaPlayer1.Ctlcontrols.stop()
        Else
            AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
            currentTrack += 1
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If (FolderBrowserDialog1.ShowDialog = DialogResult.OK) Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        ListBox1.Items.Clear()
        If Not TextBox1.Text = "" Then
            For Each file As String In My.Computer.FileSystem.GetFiles(TextBox1.Text, FileIO.SearchOption.SearchTopLevelOnly, "*.mp3")
                ListBox1.Items.Add(file)
            Next
        End If
    End Sub

    Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
        If (AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped) Then
            If (ListBox1.SelectedIndex < ListBox1.Items.Count + 1) Then
                ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1
                AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
                AxWindowsMediaPlayer1.Ctlcontrols.play()
            End If
        End If
    End Sub
End Class
 
Mp3 player example sourch code:
Imports System.IO
Imports AxWMPLib
Imports VirtualSoftware
Imports Microsoft.Win32

Public Class Form1
    Private files As New OpenFileDialog()
#Region " DEĞİŞKEN TANIMLAMA YERİ...."
    'Form Taşıma
    Const i As Integer = &HA1
    Const j As Integer = 2
#End Region

    Private Sub btnOpenFile_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click
        files.Multiselect = True
        files.Filter = "MP3 File |*.mp3"
        If files.ShowDialog = DialogResult.OK Then
            For Each o In files.SafeFileNames
                LBox1.Items.Add(o)
                Lbox3.Items.Add(o)
            Next
            For Each n In files.FileNames
                LBox2.Items.Add(n)
            Next
        End If



    End Sub
    Private Sub Basla()

        Dim Sira_ParcaSayisi As String 'Kayan Yazıda Çalan indexi ve liste sayısını gösterir
        Sira_ParcaSayisi = " - ( " & Me.LBox1.SelectedIndex + 1 & " / " & Me.LBox1.Items.Count & " )"
        Me.kayanYazi.Text = Space(40) + LBox1.Text & Sira_ParcaSayisi + Space(40)
        Timer1.Enabled = True
        PicBox1.Visible = True
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        On Error Resume Next

        Dim tlx As String
        Dim x As Integer
        tlx = ""
        For x = 1 To kayanYazi.Text.Length - 1
            tlx = tlx + Me.kayanYazi.Text(x)
        Next
        Me.kayanYazi.Text = tlx + Me.kayanYazi.Text(0)
        Label1.Text = MediaPlayer1.currentMedia.durationString
        Label2.Text = MediaPlayer1.Ctlcontrols.currentPositionString
        ColorSlider1.Maximum = MediaPlayer1.currentMedia.duration
        ColorSlider1.Value = MediaPlayer1.Ctlcontrols.currentPosition
        If CheckBox1.Checked = True Then
            Timer2.Enabled = True
        Else
            Timer2.Enabled = False
        End If

    End Sub

    Private Sub LBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBox1.SelectedIndexChanged

        Dim i As Integer
        Try
            For i = 0 To (Lbox3.Items.Count - 1)
                If Lbox3.Items(i) = LBox1.Items(LBox1.SelectedIndex) Then
                    Lbox3.SelectedIndex = Lbox3.FindString(LBox1.Text)
                    LBox2.SelectedIndex = Lbox3.SelectedIndex
                End If
            Next
            MediaPlayer1.URL = LBox2.Text
            Basla()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ColorSlider1_Scroll(sender As Object, e As ScrollEventArgs) Handles ColorSlider1.Scroll
        MediaPlayer1.Ctlcontrols.currentPosition = ColorSlider1.Value
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Width = 292
        Me.Height = 388
        ColorSlider2.Maximum = 100
        ColorSlider2.Value = MediaPlayer1.settings.volume
        PicBox1.Visible = False
        PictureBox1.Size = New Size(280, 28)
        listeYukle()
    End Sub
    Private Sub listeYukle()
        If Dir(Application.StartupPath & "\liste.dat") <> "" Then
            Dim lines = File.ReadAllLines(Application.StartupPath & "\liste.dat")
            LBox_Birlestir.Items.Clear()
            LBox_Birlestir.Items.AddRange(lines)
            LBox1.Items.Clear()
            LBox2.Items.Clear()
            Lbox3.Items.Clear()
            '--------- Muzik adlarının ve adreslerinin ayrıştırılması
            For x = 0 To LBox_Birlestir.Items.Count - 1 Step 2
                LBox1.Items.Add(LBox_Birlestir.Items(x).ToString)
                Lbox3.Items.Add(LBox_Birlestir.Items(x).ToString)
                LBox2.Items.Add(LBox_Birlestir.Items(x + 1).ToString)
            Next
        End If
    End Sub

    Private Sub ColorSlider2_Scroll(sender As Object, e As ScrollEventArgs) Handles ColorSlider2.Scroll
        MediaPlayer1.settings.volume = ColorSlider2.Value
        Button8.Image = My.Resources.ses_acik_icon
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Me.Close()
    End Sub
    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
        'Picturebox1 den formu taşıma
        PictureBox1.Capture = False
        Dim msg As Message = Message.Create(Me.Handle, i, New IntPtr(j), IntPtr.Zero)
        Me.DefWndProc(msg)

    End Sub

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        'Form1 başlıktan taşıma
        If e.Button = MouseButtons.Left Then
            Me.Capture = False  'Panelden,pictureden,textten,vb tut sürükle
            Dim msg As Message = Message.Create(Me.Handle, i, New IntPtr(j), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If MediaPlayer1.settings.mute = False Then
            Button8.Image = My.Resources.ses_kapalı_icon
            MediaPlayer1.settings.mute = True
        Else
            Button8.Image = My.Resources.ses_acik_icon
            MediaPlayer1.settings.mute = False
        End If
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        If MediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
            If LBox1.SelectedIndex = LBox1.Items.Count - 1 Then Return
            LBox1.SelectedIndex += 1
        End If
    End Sub
    Private Sub durdur()
        Timer1.Enabled = False
        Timer2.Enabled = False
        MediaPlayer1.Ctlcontrols.stop()
        Label2.Text = MediaPlayer1.Ctlcontrols.currentPositionString
        PicBox1.Visible = False
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        durdur()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Basla()
        MediaPlayer1.Ctlcontrols.play()
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        MediaPlayer1.Ctlcontrols.pause()
    End Sub

    Private Sub MediaPlayer1_PlayStateChange(sender As Object, e As _WMPOCXEvents_PlayStateChangeEvent) Handles MediaPlayer1.PlayStateChange
        If MediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
            PicBox1.Visible = False
        End If
    End Sub

    Private Sub Aratext_TextChanged(sender As Object, e As EventArgs) Handles Aratext.TextChanged
        ''Dosya araması yapılıyor. Listbox1 içinde tam bir arama yapılıyor.
        Try
            Dim bulunan As Integer = 0
            Dim i As Integer
            LBox1.Items.Clear()

            For i = 0 To Lbox3.Items.Count - 1
                If Lbox3.Items(i).ToString.ToLower.IndexOf(Aratext.Text.ToLower) >= 0 Then
                    bulunan += 1
                    Lbox3.SelectedIndex = i
                    LBox1.Items.Add(Lbox3.Items(i))
                End If
            Next
            'LBox1.SelectedIndex = 0
            Label3.Text = "Bulunan :" & bulunan
            If bulunan = 0 Then MsgBox("Böyle bir kelime yok")
        Catch ex As Exception

        End Try


    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If LBox1.SelectedIndex = LBox1.Items.Count - 1 Then Return
        LBox1.SelectedIndex += 1
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If LBox1.SelectedIndex = 0 Then Return
        LBox1.SelectedIndex -= 1
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        LBox_Birlestir.Items.Clear()

        For a = 0 To LBox2.Items.Count - 1
            LBox_Birlestir.Items.Add(Lbox3.Items(a))
            LBox_Birlestir.Items.Add(LBox2.Items(a))
        Next

        Using writer = New StreamWriter(Application.StartupPath & "\liste.dat")
            For Each o As Object In LBox_Birlestir.Items
                writer.WriteLine(o)
            Next
        End Using
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        LBox1.Items.Clear()
        LBox2.Items.Clear()
        Lbox3.Items.Clear()
        LBox_Birlestir.Items.Clear()
        btnSave_Click(Me, e)
    End Sub
End Class
mpplayer.jpeg
 
Back
Top