locked
Failed to play the music with WMP and some little code RRS feed

  • Question

  • I want to ask, before I make a personal music player app, I have this code to insert into my music in the listbox, but when I get rid of the extensions I can not play it. This is the code to add to the listbox. 

    	ListBox1.Items.Clear()
            Dim grade As New IO.DirectoryInfo("c:\mymusic")
            Dim numo As IO.FileInfo() = grade.GetFiles()
            Dim aon As IO.FileInfo
            For Each aon In numo
                ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(aon.FullName))
            Next

    and this is my code to play my music:

    	AxWindowsMediaPlayer1.URL = CType(ListBox1.SelectedItem, IO.FileInfo).FullName

     


    where lies the fault? how can I play my music with the music adding code as above? 

    Thanks for your help. 


    • Edited by Mark Cens Friday, May 23, 2014 7:55 AM false writting
    Friday, May 23, 2014 7:44 AM

Answers

  • 
    

    Option Strict On
    
    Imports System.IO
    
    Public Class Form1
    
        Dim Temp As New List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Temp.Clear()
            ListBox1.Items.Clear()
            Dim grade As New IO.DirectoryInfo("C:\Users\John\Music")
            Dim numo As IO.FileInfo() = grade.GetFiles()
            Dim aon As IO.FileInfo
            For Each aon In numo
                ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(aon.FullName))
                Temp.Add(aon.FullName)
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            For Each Item In Temp
                If Item.ToString.Contains(ListBox1.SelectedItem.ToString) Then
                    Label1.Text = Item
                End If
            Next
        End Sub
    End Class
    


    La vida loca


    Friday, May 23, 2014 3:42 PM
  • You'll need to store more information for each listbox item than just the file name without extension... you also need the full path.

    Consider just adding the FileInfo instance to the listbox and setting the DisplayMember property to "Name".  Then you can cast the listbox selecteditem back into a FileInfo and access the FullName.  It is almost what you have now, except you need to just add the FileInfo and set the display member:

    Public Class Form1
        Friend WithEvents ListBox1 As New ListBox With {.Dock = DockStyle.Fill, .IntegralHeight = False}
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(ListBox1)
            ListBox1.DisplayMember = "Name"
    
            Dim dirInfo As New System.IO.DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyMusic)
            For Each info As System.IO.FileInfo In dirInfo.GetFiles("*", IO.SearchOption.AllDirectories)
                ListBox1.Items.Add(info)
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            If ListBox1.SelectedIndex > -1 Then
                Dim info As System.IO.FileInfo = CType(ListBox1.SelectedItem, System.IO.FileInfo)
                MessageBox.Show(info.FullName)
            End If
        End Sub
    End Class
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 23, 2014 3:45 PM

All replies

  • What is that code supposed to do?

    You're not providing a path to the filename shown in the ListBox. Also you should convert a ListBox item to a string if you are going to use the item in a string. ListBox items are object types not string types.

    The only way I know to get a path to a filename is search all directories for the filename until a match occurs. But that will not necessarily return the original path you used to get the filename. So the path that is returned may be for a different file with the same name.

    Although when you add filnames to the ListBox you could add the entire path and filename to a list(Of String). Then when an item is selected in the ListBox you could search every item in the List(Of String) to see if it contains the ListBoxs Selected Item.ToString. And if so use that item to provide the URL entry for WMP to use to play a song.


    La vida loca

    Friday, May 23, 2014 3:34 PM
  • 
    

    Option Strict On
    
    Imports System.IO
    
    Public Class Form1
    
        Dim Temp As New List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Temp.Clear()
            ListBox1.Items.Clear()
            Dim grade As New IO.DirectoryInfo("C:\Users\John\Music")
            Dim numo As IO.FileInfo() = grade.GetFiles()
            Dim aon As IO.FileInfo
            For Each aon In numo
                ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(aon.FullName))
                Temp.Add(aon.FullName)
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            For Each Item In Temp
                If Item.ToString.Contains(ListBox1.SelectedItem.ToString) Then
                    Label1.Text = Item
                End If
            Next
        End Sub
    End Class
    


    La vida loca


    Friday, May 23, 2014 3:42 PM
  • You'll need to store more information for each listbox item than just the file name without extension... you also need the full path.

    Consider just adding the FileInfo instance to the listbox and setting the DisplayMember property to "Name".  Then you can cast the listbox selecteditem back into a FileInfo and access the FullName.  It is almost what you have now, except you need to just add the FileInfo and set the display member:

    Public Class Form1
        Friend WithEvents ListBox1 As New ListBox With {.Dock = DockStyle.Fill, .IntegralHeight = False}
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(ListBox1)
            ListBox1.DisplayMember = "Name"
    
            Dim dirInfo As New System.IO.DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyMusic)
            For Each info As System.IO.FileInfo In dirInfo.GetFiles("*", IO.SearchOption.AllDirectories)
                ListBox1.Items.Add(info)
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            If ListBox1.SelectedIndex > -1 Then
                Dim info As System.IO.FileInfo = CType(ListBox1.SelectedItem, System.IO.FileInfo)
                MessageBox.Show(info.FullName)
            End If
        End Sub
    End Class
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 23, 2014 3:45 PM
  • Thanks Mr. I'm very thanks youre help me..

    You inspire me..

    Sunday, May 25, 2014 11:03 PM
  • Thanks Mr. Kimble, I hope better in the future, I just want to learning..

    Youre great..

    Sunday, May 25, 2014 11:04 PM
  • Thanks Mr. I'm very thanks youre help me..

    You inspire me..


    If somebodies post or multiple posts answered your question then please propose it or them as the answer or answers. In order to show this thread was answered for anybody else searching for an answer to a similar question.

    La vida loca


    Sunday, May 25, 2014 11:04 PM