none
Play list of Mp3 in VB.NET?

    Question

  • Hello,

    I need to create a VB.NET application which playes a song at a time from a predefined list of song.Basically, there is a list of Mp3 files which is completely hidden to the user and there are 5 buttons on the screen. Each button correspond to a different Mp3 and when the user presses one of them the song associated to that button starts to play. The time-elapse of each song is unknown and there is a timer which stops the song after 10 seconds of the button-event.

    Any suggestions? I found several tutorial about Mp3 players but I could not find how to handle this kind of "playlist".

    Cheers

        Giacomo

     

    Tuesday, October 26, 2010 10:58 AM

Answers

  • Hi Giacomo,

     

    Welcome to MSDN forums!

     

    Using Windows Media Player ActiveX control to play some mp3 files combining the following two points, you'll solve your questions.

     

    1. The Windows Media Player ActiveX control can be used to calculate audio or video file duration/length.


    Firstly, add this control to ToolBox

    Right-click the Toolbox -> Choose item -> COM Components -> add Windows Media Player component

    Then you will see Windows Media Player control on Toolbox, drag&drop it to Form1, AxWindowsMediaPlayer1 object will be created.

    Code sample: Count down the media playing duration and display remaining time in Label.
    Prerequisite: AxWindowsMediaPlayer1, Timer1 and Label1on Form1.

    PublicClassForm1

     

    PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load

    Timer1.Interval=1000

    Timer1.Start()

    AxWindowsMediaPlayer1.URL="D:\VBproject\intro.wav"

    EndSub

     

    PrivateSubTimer1_Tick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesTimer1.Tick

    DimremainAsInt32=AxWindowsMediaPlayer1.currentMedia.duration-AxWindowsMediaPlayer1.Ctlcontrols.currentPosition

    DimMinutes,Seconds,ElapsedTimeAsString

    Minutes=(remain/60).ToString("00")

    Seconds=(remainMod60).ToString("00.00")

    ElapsedTime=Minutes&"."&Seconds

    Label1.Text=ElapsedTime' Displaythe remaining time for the .wav file in a label in mm:ss format

    EndSub

     

    EndClass

     

    2. To play multiple video files continuously and automatically, the key is to create the MediaPlayer Playlist object.

    Code sample: Open multiple video files and add to MediaPlayer Playlist object. 
    Prerequisites: AxWindowsMediaPlayer1, openFileDialog1 and Button1 on Form1.

    Imports WMPLib  

    Public Class Form1  

     

        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  

            Dim openFileDialog1 As New OpenFileDialog()  

            openFileDialog1.InitialDirectory = "c:\" 

            openFileDialog1.Multiselect = True 

            openFileDialog1.Filter = "AVI files (*.avi)|*.avi|WMV files (*wmv) |*.wmv|All files (*.*)|*.*" 

            openFileDialog1.FilterIndex = 1  

     

            If openFileDialog1.ShowDialog() = DialogResult.OK Then 

                ' Create Playlist for AxWindowsMediaPlayer1     

                Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist")  

                For Each FilePath As String In openFileDialog1.FileNames  

                    Dim VideoFile As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(FilePath)  

                    Playlist.appendItem(VideoFile)  

                Next 

                AxWindowsMediaPlayer1.currentPlaylist = Playlist  

            End If 

        End Sub 

     

    End Class 

     

    Another code sample: play multiple video files continuously and automatically, and highlighten current playing item in Listbox.
    Thread: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/68a445ba-9e3f-45c3-9f4a-ac8e9cf4af7d/

    Imports System.IO  

    Imports WMPLib  

    Public Class Form1  

     

    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  

     ' Retrieve file list from PlayList.txt file to ListBox1  

            Using sr As New StreamReader("C:\PlayList.txt")  

                Dim item As String = sr.ReadLine  

                While item <> Nothing 

                    ListBox1.Items.Add(item)  

                    item = sr.ReadLine  

                End While 

            End Using  

     

      ' Create Playlist for AxWindowsMediaPlayer1  

            Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist")  

            For Each FilePath As String In ListBox1.Items  

                Dim VideoFile As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(FilePath)  

                Playlist.appendItem(VideoFile)  

            Next 

            AxWindowsMediaPlayer1.currentPlaylist = Playlist  

        End Sub 

     

        ' Handle AxWindowsMediaPlayer1_CurrentItemChange event to play automatically next item  

        Private Sub AxWindowsMediaPlayer1_CurrentItemChange(ByVal sender As System.ObjectByVal e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles AxWindowsMediaPlayer1.CurrentItemChange  

            For i As Integer = 0 To ListBox1.Items.Count - 1  

                ListBox1.SelectedIndex = i ' Highlighten current playing item in ListBox1  

            Next 

        End Sub 

    End Class 

     

     

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, November 03, 2010 7:08 AM
    Moderator

All replies

  • Hi Gaicomo,

    I have moved your post from Windows Presentation Foundation(WPF) forum to Visual Basic General forum where you could get better and quicker support.

    Thanks for your understanding!

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, October 29, 2010 5:21 AM
    Moderator
  • Hi Giacomo,

     

    Welcome to MSDN forums!

     

    Using Windows Media Player ActiveX control to play some mp3 files combining the following two points, you'll solve your questions.

     

    1. The Windows Media Player ActiveX control can be used to calculate audio or video file duration/length.


    Firstly, add this control to ToolBox

    Right-click the Toolbox -> Choose item -> COM Components -> add Windows Media Player component

    Then you will see Windows Media Player control on Toolbox, drag&drop it to Form1, AxWindowsMediaPlayer1 object will be created.

    Code sample: Count down the media playing duration and display remaining time in Label.
    Prerequisite: AxWindowsMediaPlayer1, Timer1 and Label1on Form1.

    PublicClassForm1

     

    PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load

    Timer1.Interval=1000

    Timer1.Start()

    AxWindowsMediaPlayer1.URL="D:\VBproject\intro.wav"

    EndSub

     

    PrivateSubTimer1_Tick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesTimer1.Tick

    DimremainAsInt32=AxWindowsMediaPlayer1.currentMedia.duration-AxWindowsMediaPlayer1.Ctlcontrols.currentPosition

    DimMinutes,Seconds,ElapsedTimeAsString

    Minutes=(remain/60).ToString("00")

    Seconds=(remainMod60).ToString("00.00")

    ElapsedTime=Minutes&"."&Seconds

    Label1.Text=ElapsedTime' Displaythe remaining time for the .wav file in a label in mm:ss format

    EndSub

     

    EndClass

     

    2. To play multiple video files continuously and automatically, the key is to create the MediaPlayer Playlist object.

    Code sample: Open multiple video files and add to MediaPlayer Playlist object. 
    Prerequisites: AxWindowsMediaPlayer1, openFileDialog1 and Button1 on Form1.

    Imports WMPLib  

    Public Class Form1  

     

        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  

            Dim openFileDialog1 As New OpenFileDialog()  

            openFileDialog1.InitialDirectory = "c:\" 

            openFileDialog1.Multiselect = True 

            openFileDialog1.Filter = "AVI files (*.avi)|*.avi|WMV files (*wmv) |*.wmv|All files (*.*)|*.*" 

            openFileDialog1.FilterIndex = 1  

     

            If openFileDialog1.ShowDialog() = DialogResult.OK Then 

                ' Create Playlist for AxWindowsMediaPlayer1     

                Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist")  

                For Each FilePath As String In openFileDialog1.FileNames  

                    Dim VideoFile As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(FilePath)  

                    Playlist.appendItem(VideoFile)  

                Next 

                AxWindowsMediaPlayer1.currentPlaylist = Playlist  

            End If 

        End Sub 

     

    End Class 

     

    Another code sample: play multiple video files continuously and automatically, and highlighten current playing item in Listbox.
    Thread: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/68a445ba-9e3f-45c3-9f4a-ac8e9cf4af7d/

    Imports System.IO  

    Imports WMPLib  

    Public Class Form1  

     

    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  

     ' Retrieve file list from PlayList.txt file to ListBox1  

            Using sr As New StreamReader("C:\PlayList.txt")  

                Dim item As String = sr.ReadLine  

                While item <> Nothing 

                    ListBox1.Items.Add(item)  

                    item = sr.ReadLine  

                End While 

            End Using  

     

      ' Create Playlist for AxWindowsMediaPlayer1  

            Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist")  

            For Each FilePath As String In ListBox1.Items  

                Dim VideoFile As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(FilePath)  

                Playlist.appendItem(VideoFile)  

            Next 

            AxWindowsMediaPlayer1.currentPlaylist = Playlist  

        End Sub 

     

        ' Handle AxWindowsMediaPlayer1_CurrentItemChange event to play automatically next item  

        Private Sub AxWindowsMediaPlayer1_CurrentItemChange(ByVal sender As System.ObjectByVal e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles AxWindowsMediaPlayer1.CurrentItemChange  

            For i As Integer = 0 To ListBox1.Items.Count - 1  

                ListBox1.SelectedIndex = i ' Highlighten current playing item in ListBox1  

            Next 

        End Sub 

    End Class 

     

     

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, November 03, 2010 7:08 AM
    Moderator
  • Hi Giacomo,

     

    Welcome to MSDN forums!

     

    Using Windows Media Player ActiveX control to play some mp3 files combining the following two points, you'll solve your questions.

     

    1. The Windows Media Player ActiveX control can be used to calculate audio or video file duration/length.


    Firstly, add this control to ToolBox

    Right-click the Toolbox -> Choose item -> COM Components -> add Windows Media Player component

    Then you will see Windows Media Player control on Toolbox, drag&drop it to Form1, AxWindowsMediaPlayer1 object will be created.

    Code sample: Count down the media playing duration and display remaining time in Label.
    Prerequisite: AxWindowsMediaPlayer1, Timer1 and Label1on Form1.

     

    PublicClassForm1

     

    PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load

    Timer1.Interval=1000

    Timer1.Start()

    AxWindowsMediaPlayer1.URL="D:\VBproject\intro.wav"

    EndSub

     

    PrivateSubTimer1_Tick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesTimer1.Tick

    DimremainAsInt32=AxWindowsMediaPlayer1.currentMedia.duration-AxWindowsMediaPlayer1.Ctlcontrols.currentPosition

    DimMinutes,Seconds,ElapsedTimeAsString

    Minutes=(remain/60).ToString("00")

    Seconds=(remainMod60).ToString("00.00")

    ElapsedTime=Minutes&"."&Seconds

    Label1.Text=ElapsedTime' Displaythe remaining time for the .wav file in a label in mm:ss format

    EndSub

     

    EndClass

     

     

    2. To play multiple video files continuously and automatically, the key is to create the MediaPlayer Playlist object.

    Code sample: Open multiple video files and add to MediaPlayer Playlist object. 
    Prerequisites: AxWindowsMediaPlayer1, openFileDialog1 and Button1 on Form1.

     

    Imports WMPLib  

    Public Class Form1  

     

        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  

            Dim openFileDialog1 As New OpenFileDialog()  

            openFileDialog1.InitialDirectory = "c:\" 

            openFileDialog1.Multiselect = True 

            openFileDialog1.Filter = "AVI files (*.avi)|*.avi|WMV files (*wmv) |*.wmv|All files (*.*)|*.*" 

            openFileDialog1.FilterIndex = 1  

     

            If openFileDialog1.ShowDialog() = DialogResult.OK Then 

                ' Create Playlist for AxWindowsMediaPlayer1     

                Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist")  

                For Each FilePath As String In openFileDialog1.FileNames  

                    Dim VideoFile As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(FilePath)  

                    Playlist.appendItem(VideoFile)  

                Next 

                AxWindowsMediaPlayer1.currentPlaylist = Playlist  

            End If 

        End Sub 

     

    End Class 

     

     

    Another code sample: play multiple video files continuously and automatically, and highlighten current playing item in Listbox.
    Thread: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/68a445ba-9e3f-45c3-9f4a-ac8e9cf4af7d/

     

    Imports System.IO  

    Imports WMPLib  

    Public Class Form1  

     

    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  

     ' Retrieve file list from PlayList.txt file to ListBox1  

            Using sr As New StreamReader("C:\PlayList.txt")  

                Dim item As String = sr.ReadLine  

                While item <> Nothing 

                    ListBox1.Items.Add(item)  

                    item = sr.ReadLine  

                End While 

            End Using  

     

      ' Create Playlist for AxWindowsMediaPlayer1  

            Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist")  

            For Each FilePath As String In ListBox1.Items  

                Dim VideoFile As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(FilePath)  

                Playlist.appendItem(VideoFile)  

            Next 

            AxWindowsMediaPlayer1.currentPlaylist = Playlist  

        End Sub 

     

        ' Handle AxWindowsMediaPlayer1_CurrentItemChange event to play automatically next item  

        Private Sub AxWindowsMediaPlayer1_CurrentItemChange(ByVal sender As System.ObjectByVal e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles AxWindowsMediaPlayer1.CurrentItemChange  

            For i As Integer = 0 To ListBox1.Items.Count - 1  

                ListBox1.SelectedIndex = i ' Highlighten current playing item in ListBox1  

            Next 

        End Sub 

    End Class 

     

     

     

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


    bonjour je suis francais

    je voudrais jouer un album complet a partir d'un fichier ou d'un fichier wpl

    hello I'm french

    I would like to play a full album from a file or a file wpl

    wmp.URL = "D:\MUSIQUES\Dalida - 40 Succs En Or\CD 01\Dalida - 40 Succs En Or - CD 01.mp3"

    Lecteur.wmp.Ctlcontrols.play()

    ou un fichier wpl

    or a file wpl

     

    wmp.URL =

    "C:\Documents and Settings\pat\Mes documents\Ma musique\Mes sélections\selection1.wpl"

    Lecteur.wmp.Ctlcontrols.play()

    Sunday, November 14, 2010 11:05 PM