B4J Question Button callback and timing issue

Damjan

Member
Hi,

I'm coding a small control board for vMix. Basically ti sends http POST to vMix API to start or stop the recording of a video. Here is the code:

B4X:
Sub recordBtn_Action
    Dim recordingDirPath = readSettings.Get("recordingPath") As String
    Dim vMixControllerUrl = "http://" & readSettings.Get("vMixWebControllerIP") & ":" & readSettings.Get("vMixWebControllerPort") &"/api/" As String
    If recordBtn.Text = "RECORD" And fightNumber > 0 Then
        vMixWebController1.PostString(vMixControllerUrl, "Function=StartRecording")
        Try
            vMixDone(vMixWebController1)
            recordBtn.Text = "STOP"
            fightBtn.Enabled = False
            fightBtn.TextColor = disabledColor
            replayBtn.Enabled = False
            replayBtn.TextColor = disabledColor
        Catch
            Log("vMixStartRecording error:")
            Log(LastException.Message)
        End Try
    Else
        recordBtn.Text = "RECORD"
        vMixWebController2.PostString(vMixControllerUrl, "Function=StopRecording")
        Try
            vMixDone(vMixWebController2)
        Catch
            Log("vMixStopRecording error:")
            Log(LastException.Message)
        End Try
        Delay(2000)
        fightBtn.Enabled = True
        fightBtn.TextColor = defaultColor
        replayBtn.Enabled = True
        replayBtn.TextColor = defaultColor
        Dim lastModifiedFile As String
        Try
            lastModifiedFile = getLastModifiedFile(recordingDirPath)
            renameLastRecordedFile(recordingDirPath, lastModifiedFile ,addLeadingZeros(fightNumber,4) &"-"& addLeadingZeros(breakNumber,2) &".mp4")
            updateFileList
        Catch
            Log("Rename last file error:")
            Log(LastException.Message)
        End Try
    End If
End Sub

Problem is that delay is not called after the http POST is done but http POST is waiting for the delay and then completes. I need to rename and delete files after the job is done but now it rename it immediately before vMix stops recording so there is missing EOF and the file is corrupted. It is mp4 file.

If my code is correct it should do it like this:
  1. Start recording
  2. Stop recording
  3. Delay for 2 sec
  4. Rename file (copy it with a new name)
  5. Delete the old file
But now it works like this:
  1. Start recording
  2. Delay for 2 sec
  3. Stop recording, Rename, Delete - this is happening in the same time and corrupts the file
What am I doing wrong here??

Thank you!!
 
Top