B4J Question [Solved] Watchdog timer for mediaplayer

bdunkleysmith

Active Member
Licensed User
Longtime User
I have an app which displays short basketball player videos or images sequentially as part of a pre-game presentation. It works very well, however sometimes a MediaException is thrown by the Mediaplayer if any of the video files are corrupted.

I use:

B4X:
wait For mppp_Complete

to control the sequencing and so the app hangs if the above exception is thrown because the mppp_Complete event is not is raised.

I have tried to use Try / Catch to capture that event, but that does not seem possible and so I tried to implement a watchdog timer in that if a nominal time which is slightly longer than the videos elapses, then I could raise the mppp_Complete event so the app would continue onto the next player video.

Here is the code I tried:

B4X:
Sub PlayerIntros
    ' Show videos or images of players in that preferential order
    For Each k As Object In mapHomeTeam.Keys
        Dim playerId As String = mapHomeTeam.Get(k)
        For a = 0 To mapHomeTeam.Size - 1
        If playerId = arrayHome(a, 0) Then   
                Dim mediaFile As String = arrayHome(a, 1).SubString(arrayHome(a, 1).IndexOf(" ") + 2)                                   
                If IntroVideos(mediaFile & ".mp4", arrayHome(a, 1).SubString2(0, 2).Trim) = True Then
                    Timer5.Enabled = True

                    wait For mppp_Complete

                    Timer5.Enabled = False
                    mMediaViewNode.RemoveNodeFromParent
                Else if IntroImages(mediaFile & ".png", arrayHome(a, 1).SubString2(0, 2).Trim) = True Then
                    Sleep(txtIntrosDelay.text * 1000)
                Else
                    IntroImages("noImageFile.png", arrayHome(a, 1).SubString2(0, 2).Trim)
                    Sleep(txtIntrosDelay.text * 1000)
                End If
        End If
        Next
    Next
End Sub

Sub Timer5_tick   
    Log("Error" & Display.playerNumber.text)
    mppp_Complete
End Sub

Sub mppp_Complete
    Log("mppp_Complete")
End Sub

Sub IntroVideos(videoFile As String, pNum As String ) As Boolean    'firstname dot space secondname, player singlet number
    Display.playerNumber.text = pNum
    Display.playerNumber.top = Display.ivGraphics.Top
    Display.playerNumber.left = Display.ivGraphics.Left + (Display.ivGraphics.Width / 50)
    Display.playerNumber.TextSize = playerNumberFontSize * Display.ivGraphics.Width / 1920
    Display.playerNumber.visible = True    
    If File.Exists(PublicApplicationDataFolder & "\Intros\" & cmbOrganisationID.Value, videoFile) = True Then        
        mppp.Initialize("mppp",File.GetUri(PublicApplicationDataFolder & "\Intros\" & cmbOrganisationID.Value, videoFile))
        mv.InitializeNewInstance("javafx.scene.media.MediaView",Array(mppp))
        mMediaViewNode = mv
        Display.fullPane.AddNode(mMediaViewNode,0,0,-1,-1)
        mv.RunMethod("setFitWidth",Array(videoWidth))
        mv.RunMethod("setFitHeight",Array(videoWidth*9/16))
        mMediaViewNode.Visible = True
        mppp.play        
        Display.playernumber.As(B4XView).BringToFront
        Return True
    Else
        Display.playernumber.As(B4XView).BringToFront
        Return False
    End If
End Sub


But it seems that my line in the Timer5_tick sub:

mppp_Complete:
mppp_Complete

just calls the mppp_Complete sub and does not raise the mppp_Complete event in order to move past

B4X:
wait For mppp_Complete

So is there a way for me to raise the mppp_Complete event in code?

Alternatively I thought:

B4X:
wait For mppp_Complete Or wait For Timer5_tick

might be a way to achieve my objective, but it seems that is not acceptable syntax.

I seem to be chasing my tail and so any suggestions / guidance would be much appreciated.
 
Solution
From where and how are you calling PlayerIntros? With 'Wait For..'?

I'm not sure that this will make any difference but you could try:
B4X:
Sub PlayerIntros
    ' Show videos or images of players in that preferential order
    For Each k As Object In mapHomeTeam.Keys
        Dim playerId As String = mapHomeTeam.Get(k)
        For a = 0 To mapHomeTeam.Size - 1
        If playerId = arrayHome(a, 0) Then   
                Dim mediaFile As String = arrayHome(a, 1).SubString(arrayHome(a, 1).IndexOf(" ") + 2)                                   
                If IntroVideos(mediaFile & ".mp4", arrayHome(a, 1).SubString2(0, 2).Trim) = True Then
                    Timer5.Enabled = True

                    wait For video_Complete...

Chris2

Active Member
Licensed User
From where and how are you calling PlayerIntros? With 'Wait For..'?

I'm not sure that this will make any difference but you could try:
B4X:
Sub PlayerIntros
    ' Show videos or images of players in that preferential order
    For Each k As Object In mapHomeTeam.Keys
        Dim playerId As String = mapHomeTeam.Get(k)
        For a = 0 To mapHomeTeam.Size - 1
        If playerId = arrayHome(a, 0) Then   
                Dim mediaFile As String = arrayHome(a, 1).SubString(arrayHome(a, 1).IndexOf(" ") + 2)                                   
                If IntroVideos(mediaFile & ".mp4", arrayHome(a, 1).SubString2(0, 2).Trim) = True Then
                    Timer5.Enabled = True

                    wait For video_Complete        'change here

                    Timer5.Enabled = False
                    mMediaViewNode.RemoveNodeFromParent
                Else if IntroImages(mediaFile & ".png", arrayHome(a, 1).SubString2(0, 2).Trim) = True Then
                    Sleep(txtIntrosDelay.text * 1000)
                Else
                    IntroImages("noImageFile.png", arrayHome(a, 1).SubString2(0, 2).Trim)
                    Sleep(txtIntrosDelay.text * 1000)
                End If
        End If
        Next
    Next
End Sub

Sub Timer5_tick   
    Log("Error" & Display.playerNumber.text)
    CallSubDelayed(Me, "video_Complete")    'added
End Sub

Sub mppp_Complete
    Log("mppp_Complete")
    CallSubDelayed(Me, "video_Complete")        'added
End Sub

Also, if you don't mind perhaps getting involved in slightly more major changes, have a look at these two threads:
 
Upvote 0
Solution

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks @Chris2. Your suggestion works perfectly. Obviously I need to understand the use of CallSubDelayed and I'll also digest the other two references you've provided.

Incidentally, PlayerIntros is on completion of an introductory video (mp_Complete). Here's a short clip of that transition I recorded to show others how this app was progressing;
 
Upvote 0
Top