Android Question How to end "For ... Next" with a button click?

MikeSimpson

Member
Licensed User
Longtime User
With a press of a button, I want to play 44 mp3 files. It is working, but how can I exit with a click on a stop button? During playing all buttons seems to be blocked until the "For .. Next" sequence is ended.

B4X:
Sub btnPlay_Click
   
       
        For i = 1 To 44    'Plays 44 mp3 files from 1.mp3 to 44.mp3
   
            MediaPlayer2.Initialize( )
            MediaPlayer2.Load(File.DirAssets, i & ".mp3")
            MediaPlayer2.Play
       
        Do While MediaPlayer2.IsPlaying = True   'Wait until the mp3 file has finished played until Next
        Loop
       
        MediaPlayer2.Release   
       
        If Ende = True Then     'With a click on the End button, I want to set "Ende" to True and exit,
        Exit                    'but it dosn't work
        End If
       
    Next
    Ende = False
   
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
With the code
B4X:
Do While MediaPlayer2.IsPlaying = True  'Wait until the mp3 file has finished played until Next
Loop
you are blocking the main-thread...

1. Put your 44 mp3´s in a list
2. Start playing the first one
3. Start a timer (every 1 second or so tick)
4. On timer tick check if the mp3 already playing or not
4.1. if is still playing do nothing
4.2. if mp3 ready release mediaplayer and start the next mp3. goto 3...

This will not block the mainthread and then your app can react on a button-click... Use a global variable to check in timer tick... If you clicked the button then stop playing the mp3 in timer tick and do not start the next mp3...
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User

Maybe not the best solution, but this looks like to work:

B4X:
For i = 1 To 44                                    'Plays 44 mp3 files from 1.mp3 to 44.mp3
   
            MediaPlayer2.Initialize( )
            MediaPlayer2.Load(File.DirAssets, i & ".mp3")
            MediaPlayer2.Play
       
        Do While MediaPlayer2.IsPlaying = True           
            DoEvents
                If Ende = True Then                               
                               
                MediaPlayer2.Release
                i = 44
                Exit   
                End If
        Loop
       
        MediaPlayer2.Release   
       
   
    Next
    Ende = False
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
This will hammer your battery. Far better to do as DonManfred or Erel has suggested. It wouldn't take much alteration of your code either. You could change the variable "i" (which you use to load tracks 1 to 44) into a global variable and in the MediaPlayer_Complete event do exactly as you're already doing in the For Next loop. You'll just need to manually increment "i" and check that is not greater than 44.

Regards,
RandomCoder
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…