Android Question Wait For in combination with If statement

Discussion in 'Android Questions' started by Syd Wright, May 8, 2018.

  1. Syd Wright

    Syd Wright Active Member Licensed User

    Yesterday I upgraded from B4A version 6.80 to 8.00. So far I was hesitant to do so because of the problems some users reported in the forum. However, it seems to work very well, apart from having to make some changes to the notification code order.

    I have now started to replace DoEvents by Sleep and Wait For code.

    Just a small question regarding the latter. I can't seem to use:

    Code:
    If a = 1 Then Wait For MediaPlayer1.IsPlaying = False
    In stead this is needed:

    Code:
    If a = 1 Then
       
    Wait For MediaPlayer1.IsPlaying = False
    End If
    Why?
     
  2. LucaMs

    LucaMs Expert Licensed User

    I don't think that the second code will work, because Wait For waits for an event, IsPlaying is instead a property.
     
  3. Syd Wright

    Syd Wright Active Member Licensed User

    Thank you. So far it seems to work, but I probably need to test it further.
    It is rather a nuisance having to replace all the routines with DoEvents, but so be it.
    In this case I simply want to make my code wait for the completion of a short sound ("dit" and "dah" in Morse) that is played using a MediaPlayer.
     
  4. Syd Wright

    Syd Wright Active Member Licensed User

    Apart from that, I assume that the one liner "If" command has not yet been created by Erel in case of using "wait for...".
     
  5. DonManfred

    DonManfred Expert Licensed User

    as far as know the compiler neet the wait for in a new line.
    I may be wrong
     
  6. Syd Wright

    Syd Wright Active Member Licensed User

    Thank you. Any thoughts on what LucaMS said above? Am I wrong to use:
    Code:
    Wait For MediaPlayer1.IsPlaying = False
    ?
    If so, I probably have not yet completely understood the Wait For command...
     
  7. LucaMs

    LucaMs Expert Licensed User

    Wait For MediaPlayer1_Complete
     
    Syd Wright likes this.
  8. DonManfred

    DonManfred Expert Licensed User

    i guess YES.
    To use wait for you need a senderfilter.
    Make your sub to be a resumeable sub and return a value here.
    See the ResumeableSub tutorial / Video.
     
  9. Syd Wright

    Syd Wright Active Member Licensed User

    I thought this was the best solution, but as it now turns out, the Complete event fires much too soon!
    Code:
    Sub WAKE_ME1
      
    For n=0 To 6
        SOUND1(
    "charge1.mp3",1)
      
    Next
      TTS1.Speak(
    "Time to get out of bed!"true)
    End Sub

    Sub SOUND1(SGeluid1 As String, Swait1 As Int)
        
    If File.Exists(File.DirAssets, SGeluid1) = False Then Return
        MediaPlayer1.Load(
    File.DirAssets, SGeluid1)
        MediaPlayer1.Play
        
    If Swait1=1 Then               
          
    Wait For MediaPlayer1_Complete
        
    End If
    End Sub
    It is very noticeable when playing a short, 2.5 seconds sound half a dozen times in a row: it only plays each sound about 0.5 seconds and then immediately starts with the next. So back to a DoEvents loop it will have to be:

    Code:
    Sub SOUND1(SGeluid1 As String, Swait1 As Int)
        
    Dim Hulp1, Hulp2 As Long
        
    If File.Exists(File.DirAssets, SGeluid1) = False Then Return
        MediaPlayer1.Load(
    File.DirAssets, SGeluid1)
        Hulp1 = MediaPlayer1.Duration-
    2      'Slightly shorter
        MediaPlayer1.Play
        
    If Hulp1<0 Then Hulp1 = 0
        
    If Swait1 = 1 Then
          
    Do While MediaPlayer1.IsPlaying
            
    DoEvents              'Replacing this by Sleep(0) or Sleep(10): same problem!
          Loop
        
    End If
    End Sub
    This has worked for many years. Replacing the DoEvents by Sleep(0) or for example Sleep(10) causes the same problem: the sub ends too soon!

    I can't say I am having much luck with the new Wait For... and Sleep() commands: When replacing a DoEvents in another part of my app by Sleep(0) it won't show a Logo splash screen anymore (just skips it). This Sleep(0) is nowhere near the sub that is used to show the Logo in an imageview...
     
    Last edited: May 11, 2018
  10. LucaMs

    LucaMs Expert Licensed User

    This should work:
    Code:
    Sub WAKE_ME1
        
    For n = 0 To 6
            
    Wait For (SOUND1("charge1.mp3"1)) Complete (SoundPlayed As Boolean)
        
    Next
        TTS1.Speak(
    "Time to get out of bed!"True)
    End Sub


    Sub SOUND1(SGeluid1 As String, Swait1 As Int) As ResumableSub
        
    If File.Exists(File.DirAssets, SGeluid1) = False Then
            
    Return False
        
    End If
     
        MediaPlayer1.Load(
    File.DirAssets, SGeluid1)
        MediaPlayer1.Play
        
    If Swait1 = 1 Then
            
    Wait For MediaPlayer1_Complete
            
    Return True
        
    Else
            
    Return True
        
    End If
    End Sub

    [Why Swait is not a boolean?]


    [You do not necessarily have to replace DoEvents with Sleep]
     
    Last edited: May 11, 2018
    Syd Wright likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice