Here we go:
1) The first problem you are having is that you are not using the same encoding / decoding setup for the audio. In your B4A app you have
audioStream.Initialize("AudioStream", 22050, True, 16, audioStream.VOLUME_MUSIC)
and in B4J you originally had
AT.Initialize(44100,16,AT.Ch_Conf_Stereo)
There are actually two issues in the B4J statement. The first I mentioned above, the second is that in B4A you are recording in Mono (that's the True portion of the audioStream.Initialize), yet you are setting up B4J for stereo playback. So the correct line in B4J should be
AT.Initialize(22050,16,AT.Ch_Conf_Mono)
2)
In B4J I get Receiving audio. Length of Data(): 1792
Actually, what you are displaying on the B4J side is the size of the UDP buffer. That will always be the size that you set up with (as in your B4J code)
udpsock.Initialize("udpsock", port, 1792)
If instead you would have written
udpsock.Initialize("udpsock", port, 2000)
Then your Data() length would have always been 2000. And here is the other issue. The buffer size needs to be large enough to handle other recording buffer sizes. Just because your phone does 1792, does not mean others user more (or less). My 1st generation Moto-X has a recording buffer size of 2048. Would you use my phone with your setup, you would chop off over 250 bytes of audio. Just go ahead and make the buffer big enough to handle other phones. Most examples in this forum seem to use 8000
udpsock.Initialize("udpsock", port, 2000)
Note: From work I've done for someone else, it seems that the less powerful a phone's CPU is, the bigger the recording buffer is.
3)
The reason that you have no playback in your B4J application is that you did not use AT.Start (and AT.Stop). Now, this seems to get a little trickier. If you put AT.Start right after AT.Initialize (and AT.Stop in you MainForm_Closed sub), playback occurs, but at times you experience a endless loop after the B4A app finished sending data. I've developed one workaround, but please note, I'm not an expert on the jAudioTrack library, nor audio in general.
Workaround steps:
a) In your B4A app
1) Add a variable to your Process_Globals
Private sendFlagPacket(10) As Byte
2) In your SendAudio sub, add
3) In your StopSendingAudio sub add
This will signal you B4J app when to start and stop playback
b) In your B4J app, change udpsock_PacketArrived's Try block to to
Try
If(Packet.Length = 10) Then
If playbackFlag = False Then
playbackFlag = True
Log("Start playing")
AT.Start
Else
playbackFlag = False
Log("Stop playing")
AT.Drain
AT.Stop
End If
End If
If playbackFlag = True Then AT.Write(Packet.Data,Packet.Offset,Packet.Length)
Catch
Log(LastException)
End Try
Note: This may not be 100% robust, since UDP can have packet loss and the start/stop flags may be one of the packets that get lost. Nor does this handle multiple playback devices. But this at least should get you up and running.