Android Question Detect string from astream input buffer

Meigionel TS

Member
Licensed User
I am having a socket client which sends both text data as well as media (audio bytes) to my B4A app. The only text buffer the client sends is a string "200". Now based on the incoming data I am trying to trigger events. So if it is audio bytes then Audio Streamer would play the bytes however if my app's socket server receives "200" then it would show a Toastmessage.

I couldn't understand how to implement this using AsyncStreamsText so I used the non-prefixed mode of Async to get the incoming byte and then converting it to string.

The code I thought would work.

B4X:
Sub astreammedia_NewData (Buffer() As Byte)
    If sendingAudio = False Then
       
        Dim msg As String

        msg = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
        
        If msg <> "200" Then
            audioStream.Write(Buffer)
        Else
            ToastMessageShow("Worked",False)
        End If
    End If
End Sub

It checks if the byte is not "200" then it plays using audioStream but if it is 200 then a toast. Although it works to some extent but most of the time after playing the audio for a few seconds, it starts distorting and finally giving out noise and no actual audio.

I realize that there may be a better or "right way" to approach this problem, but I could not find it. Any help is appreciated. :)
 
Top