Android Question media player, album

Laurie

Member
Licensed User
Longtime User
Media player works well. Very instructional but how would I play all the songs in an album? My 'looping' attempts have all failed miserably!
 

Laurie

Member
Licensed User
Longtime User
The following plays one song only???

Sub btnPlayAlbum_Click
Dim fileList As List
Dim n As Int

fileList = File.ListFiles(path1)
fileList.Sort(True)

For n = 1 To fileList.Size-1
NewFile = fileList.Get(n)
MediaPlayer1.Load(path1,NewFile)
MediaPlayer1.Position = 0
timer1.Initialize("timer1", 1000)
MediaPlayer1.Play
Next
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub btnPlayAlbum_Click
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Like Klaus said:

This is from memory (which means it's untested and possibly wrong). Was wrong and now corrected.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

     Dim MP As MediaPlayer  ' Must be in Process_Globals

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim path1 As String
 
    Dim fileList As List
    Dim FilePntr As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    MP.Initialize2("MP")
    path1 = "Your album directory"
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnPlayAlbum_Click
 
    ' Get the list of files to play
    fileList = File.ListFiles(path1)
    fileList.Sort(True)
 
    ' If there are files, start playing the first one
    If fileList.Size>0 Then
   
        FilePntr=0
        MP.Load(path1, fileList.Get(FilePntr))
        MP.Play
   
    End If

End Sub

' Called when the current track completes
Sub MP_Complete

    ' Point to the next song
    FilePntr=FilePntr+1
 
    ' If there are more songs, start playing the next one
    If FilePntr < fileList.Size Then
        MP.Load(path1, fileList.Get(FilePntr))
        MP.Play
    Else
        ToastMessageShow("Album done!", True)
    End If
 
End Sub
 
Last edited:
Upvote 0

Laurie

Member
Licensed User
Longtime User
Many thanks, that got me going.

Did note that MP.Initialize("MP"), needed to be MP.Initialize2("MP")

Otherwise, spot on!
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Ah! Also, the MP Dim must be in Process_Globals. I've corrected both in the above for future generations to learn from ;)
 
Upvote 0
Top