B4J Question Want to play a windows media playlist ( .m3u) (SOLVED)

Colin Evans

Active Member
Licensed User
Longtime User
Whilst I can play an individual mp3 file with the following code

B4X:
Private Sub btnMusic_Action
    Log("Music btn")
    Dim mp As MediaPlayer
    mp.Initialize("mp", File.GetUri("C:\Music\Background", "OneRepublic - Counting Stars.mp3"))
    mp.Play
    Wait For (mp) mp_Complete
End Sub

I want to be able to make my way through a .m3u playlist the format, snippet taken from a look in Notepad, looks like this, could someone please give me an idea how to do this, as I keep trying and getting nowhere

Thanks in advance

B4X:
#EXTM3U
#EXTINF:0,12. Pink - Just Like Fire.mp3
Background\12. Pink - Just Like Fire.mp3

#EXTINF:0,OneRepublic - Counting Stars.mp3
Background\OneRepublic - Counting Stars.mp3

#EXTINF:0,James Morrison - Broken Strings (feat. Nelly Furtado).mp3
Background\James Morrison - Broken Strings (feat. Nelly Furtado).mp3

#EXTINF:0,Ed Sheeran - Photograph.mp3
Background\Ed Sheeran - Photograph.mp3

#EXTINF:0,Take That - Give You My Love.mp3
Background\Take That - Give You My Love.mp3

#EXTINF:0,04 Chicago - Hard Habit To Break.mp3
Background\04 Chicago - Hard Habit To Break.mp3

#EXTINF:0,11. Glassheart.mp3
Background\11. Glassheart.mp3

#EXTINF:0,09-travis-flowers_in_the_window-fnt.mp3
Background\09-travis-flowers_in_the_window-fnt.mp3

#EXTINF:0,26 All I Want.mp3
Background\26 All I Want.mp3

#EXTINF:0,Jess Glynne - Take Me Home.mp3
Background\Jess Glynne - Take Me Home.mp3

#EXTINF:0,The Weeknd - Can't Feel My Face.mp3
Background\The Weeknd - Can't Feel My Face.mp3

#EXTINF:0,08-Sunday Morning.mp3
Background\08-Sunday Morning.mp3

#EXTINF:0,Grace - You Don't Own Me.mp3
Background\Grace - You Don't Own Me.mp3
 

Colin Evans

Active Member
Licensed User
Longtime User
So I can read the .m3u file with

B4X:
    For Each line As String In File.ReadList("C:\ditl\Music", "playlist6.m3u")
        Log(line)
   Next

Is it easy to then move a line forward if the start of the line read contains #EXTINF to get the subdirectory and filename to play, i.e.

Background\OneRepublic - Counting Stars.mp3
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Couldn't work it out using the .m3u playlist so I use a little program to list all the files in a folder and saved it to a text file and its working now

B4X:
Private Sub cmbMusicPopulate
    cmbMusic.Items.Clear
    cmbMusic.Items.AddAll(Array As String("Background Music", "Rock Ballads", "Motown", "60's Music","70's Music","80's Music","90's Music","00's Music"))
End Sub

B4X:
Sub cmbMusic_selectedIndexChanged(index As Int, Value As Object)
    Dim mMusic, mFolder, mName, mfilename As String
    btnStopMp.Visible = False
    If cmbMusic.SelectedIndex = -1 Then
        Return
    Else
        btnStopMp.Visible = True
        mName = Value
        If mName = "Background Music" Then
            mFolder = "C:\ditl\music\"
            mfilename = "background.txt"
        End If
        If mName = "Rock Ballads" Then
            mFolder = "C:\ditl\music\"
            mfilename = "rock ballads.txt"
        End If
        If mName = "Motown" Then
            mFolder = "C:\ditl\music\"
            mfilename = "motown.txt"
        End If
        If mName = "60s Music" Then
            mFolder = "C:\ditl\music\"
            mfilename = "60s music.txt"
        End If
        If mName = "70s Music" Then
            mFolder = "C:\ditl\music\"
            mfilename = "70s music.txt"
        End If
        If mName = "80s Music" Then
            mFolder = "C:\ditl\music\"
            mfilename = "80s music.txt"
        End If
        If mName = "90s Music" Then
            mFolder = "C:\ditl\music\"
            mfilename = "90s music.txt"
        End If
        If mName = "00s Music" Then
            mFolder = "C:\ditl\music\"
            mfilename = "00s music.txt"
        End If
        For Each line As String In File.ReadList(mFolder, mfilename)
            mMusic = mFolder & mName
            mp.Initialize("mp", File.GetUri(mMusic, line))
            mp.Play
            Wait For (mp) mp_Complete
        Next
    End If
End Sub

B4X:
Private Sub btnStopMp_Action
    mp.Stop
End Sub

I realise it's a bit messy but it works, hope it may help anyone else, cheers
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,
according to Wikipedia, you should have your .m3u file in your Music folder (since the dir where audio files are placed is a sub-dir of it named Background, in your case).
This makes for the " local pathname relative to the M3U file location" specification type.
The correct audio filename should be placed on the row following the one sporting the # EXTINF: which is used for track length and human-readable title.

Maybe your problem derives from a malformed audio file name. Eventually you could try the full path option.

ps: I don't know if it is desidered, but reading the Wikipedia doc I assume you could mix tracks from diffent directories in the same m3u file; it could be nice for a random selection built on the fly.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thanks for the input but m3u files can be anywhere, I've been using a VB program I did years ago just to call Windows Media Player,
B4X:
Private Sub cmd1_Click()
lblPlaying.Caption = cmd1.Caption
ret = ShellExecute(Me.hWnd, vbNullString, "C:\ditl\music\playlist1.m3u", vbNullString, CurDir, 1)
End Sub

Private Sub cmd2_Click()
lblPlaying.Caption = cmd2.Caption
ret = ShellExecute(Me.hWnd, vbNullString, "C:\ditl\music\playlist2.m3u", vbNullString, CurDir, 1)
End Sub
etc
Yes you can have files from anywhere listed in the .m3u file, media player just went off the path given in the .m3u file

And I'm sure you'd probably be able to call it from B4J but didn't find anything, also I'm happy with the way things have worked out, having the operation within the app rather than calling Media Player which is a little weird at times

Cheers, Colin
 
Upvote 0

udg

Expert
Licensed User
Longtime User
m3u files can be anywhere
Sure. My indication derived by your use of a relative path in the example given above. For a full path you could place m3u file everywhere.
Look at the examples in the Wikipedia doc. There's a case (example 2) where you specify just a dir to play all the files there.
Examples 3 and 4 are the most useful, IMHO.

Anyway, great that you found a good solution for you.
For me, it was the opportunity to learn something new (and maybe experiment a bit when a coffe-break from my payed job occurs). Thank you.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Sure. My indication derived by your use of a relative path in the example given above. For a full path you could place m3u file everywhere.
Look at the examples in the Wikipedia doc. There's a case (example 2) where you specify just a dir to play all the files there.
Examples 3 and 4 are the most useful, IMHO.

To be honest, I've never used Wikipedia, just had a look and it's quite informative, thanks for the pointer, enjoy your coffee when you get chance:)
 
Upvote 0

barve5

New Member
Download your M3U File and open it utilizing a word processor.
Duplicate the URL playlist.
Then, at that point, open the VLC Media Player.
Click the Media Button and select "Open Network Stream."
Whenever a window springs up, select Network and the glue the duplicated URL.
Click Play.
 
Upvote 0
Top