Android Question sending audio by mqtt (android > java)

jose luis gudino

Active Member
Licensed User
Longtime User
Hello everyone, I have made an application to send audio by mqqt using the example of AudioStreamer

B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("1")
  
    buffers.Initialize
    timer1.Initialize("timer1", 1000)
End Sub

Private Sub streamer_RecordBuffer (Buffer() As Byte)
    buffers.Add(Buffer)
End Sub


  
Private Sub btnStartRecording_Click
    rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        xui.MsgboxAsync("No permission", "")
        Return
    End If
    If streamer.PlayerBufferSize = 0 Then
        streamer.Initialize("streamer", 22050, True, 8, streamer.VOLUME_MUSIC)
    End If
    buffers.Clear
    streamer.StartRecording
    recordingStart = DateTime.Now
    timer1.Enabled = True
    Timer1_Tick
    btnPlay.Enabled = False
End Sub

Private Sub Timer1_Tick
    Label1.Text = "Recording: " & _
        Round((DateTime.Now - recordingStart) / DateTime.TicksPerSecond) & " seconds"
End Sub

Private Sub btnStopRecording_Click
    streamer.StopRecording
    timer1.Enabled = False
    btnPlay.Enabled = True
    Label1.Text = ""
End Sub

Then send by mqtt the variable buffers of type list

B4X:
Private Sub btnPlay_Click
    btnStartRecording.Enabled = False
    streamer.StartPlaying
    For Each b() As Byte In buffers
    '    streamer.Write(b)
    Next
   ' send buffers
    CallSub3(Qmtt,"enviar_a_MQQT_chat",buffers,"Master")
    streamer.Write(Null) 'when this "message" will be processed, the player will stop.
End Sub

Now if I receive the variable and play the audio

B4X:
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    Try
        mensaje_recibido(Topic,Payload)
    Catch
    End Try
End Sub

Sub mensaje_recibido( Topic As String, Payload() As Byte)
  
    Dim obj As Object
    obj = serializator.ConvertBytesToObject(Payload)
    Dim mensajesllegado As List = obj
    CallSub2(B4XPages.MainPage,"suena_mensaje",mensajesllegado)
  
End Sub

Now if I receive the variable and play the audio

B4X:
Public Sub suena_mensaje(mensaje As List)
    ToastMessageShow("LLegando mensaje",True)
    btnStartRecording.Enabled = False
    streamer.StartPlaying
    For Each b() As Byte In mensaje
        streamer.Write(b)
    Next
    streamer.Write(Null) 'when this "message" will be processed, the player will stop.
End Sub

now, I send the buffers variable to B4J to try to play the audio
and I pass it through mqtt without problems, but
the problem is that when listening to it in B4J the audio sounds noisy and distorted

B4X:
Private Sub PlayBytes(Bytes() As Byte)
  
    Dim AIS As JavaObject = jAudioTrack2_Utils.NewAudioInputStreamFromBytes(Bytes,AudioFormat)
    PlayStream1.Start(SDL,AIS)
End Sub

Public Sub ListToBytes(list As List) As Byte()
    Dim o As OutputStream
    o.InitializeToBytesArray(0)


    Dim b() As Byte
  
    For i = 0 To list.Size - 1
        b = list.Get(i)
        o.WriteBytes(b, 0, b.Length)
    Next
  
    Dim retB() As Byte = o.ToBytesArray
  
    o.Close

    Return retB
  
End Sub

the audio has this configuration

B4X:
'Create an Audioformat Instance
    Dim SampleRateHz As Float = 22050
    Dim SampleSizeInBits As Int = 8
    Dim ChannelConfig As Int = 1
    AudioFormat = jAudioRecord2_Utils.NewAudioFormat(SampleRateHz,SampleSizeInBits,ChannelConfig,True, False)

Does anyone have an idea what it could be?

thanks in advance
 
Last edited:

Magma

Expert
Licensed User
Longtime User
Hi there...

as i can understand the problem is from the side of receive and play (not waiting the buffer to play, to play the next one)... but you are not giving the code of "send/mqtt" and mqtt generally... to help you more...

a small project with your client will describe the problem better...
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
Hi there...

as i can understand the problem is from the side of receive and play (not waiting the buffer to play, to play the next one)... but you are not giving the code of "send/mqtt" and mqtt generally... to help you more...

a small project with your client will describe the problem better...

Hello, thanks for the answer
I am going to prepare a small project and upload it, to understand the problem.
thank
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
hello.
Attached demos in B4A and B4J

in B4A the sound that comes through MQQT is perfect
While B4J the sound has a lot of noise

Thank
 

Attachments

  • Mqqt_audio.zip
    3.2 KB · Views: 71
  • AudioStreamerMQTT.zip
    17 KB · Views: 68
Upvote 0

Magma

Expert
Licensed User
Longtime User
...hmmm...

ok... b4j mqtt_audio is only for playing the received buffer from... b4a client?
no need recording ?

+ what "result" do you want... what is the purpose of app... chat of 2 clients... chat of more than two... ?... or one client record.... many clients play ?
 
Last edited:
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
...hmmm...

ok... b4j mqtt_audio is only for playing the received buffer from... b4a client?
no need recording ?

+ what "result" do you want... what is the purpose of app... chat of 2 clients... chat of more than two... ?... or one client record.... many clients play ?
Hi.
I need to play the audio and also store it :)
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi.
correct. play the sound, and the next point
it is to record the audio

one client record.... many clients play


caution at b4j - "REM" record - in b4a "REM" play:
1685728519606.png

*example screenshot from b4j....

Did that because i didn't upload two different projects - but the same... B4X rules.. ofcourse if this is tiring... you unzip the project two times... rename folder and have two different solutions :)
Have in mind... that I ve already created video-audio-text-chat meet@point (using mqtt)... so if you like this code / you can check my try for something better...

...So now you can use my example to record from a client/clients and listen from the other/others... but caution if you need to do the same time record/play you need to have headset...

ps: PlayDataAudio Class is Star-Dust patent :) + jAudioRecord v1.0 lib... ofcourse stevel05
 

Attachments

  • audiomqtt-1.zip
    14 KB · Views: 73
Upvote 0
Top