Android Tutorial Walkie Talkie - Audio streaming over Wifi or Bluetooth

Status
Not open for further replies.

Sahaya Arul

Member
Licensed User
dear Erel,
How do you send audio to all the devices connected in the wifi using walkie talkie...please help me...I need detailsed explanations please...
 

Philip Prins

Active Member
Licensed User
@ philip: Can you update the walkie talkie to an UDP Version and send it over to me...
B4X:
Sub Service_Create
    'start listening for BT and wifi connections
    server.Initialize(port, "server")
    UDPSocket1.Initialize("UDP", 3031,320)
    Try
        server.Listen
    Catch
        WifiStatus = "Error listening: " & LastException
        UpdateUI
    End Try
    admin.Initialize("admin")
    serial1.Initialize("serial1")
    If serial1.IsEnabled Then serial1.Listen2("na", uuid)
    pe.Initialize("pe")
    pe_ConnectivityChanged("", "", Null)
    audioStream.Initialize("AudioStream", 8000, True, 16, audioStream.VOLUME_MUSIC)
    audioStream.StartPlaying
End Sub

Sub admin_StateChanged (NewState As Int, OldState As Int)
    If NewState = admin.STATE_ON Then serial1.Listen2("na", uuid)
End Sub

Sub pe_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    MyIP = server.GetMyWifiIP
    UpdateUI
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub
Private Sub UpdateUI
    CallSub(Main, "UpdateUI")
End Sub

Public Sub Disconnect
    If WifiConnected Or BTConnected Then
        astream.Close
        AStream_Terminated
    End If
End Sub
Public Sub ConnectBT(Address As String)
    serial1.Connect2(Address, uuid)
    BTStatus = "Trying to connect..."
    UpdateUI
End Sub

Public Sub ConnectWifi(Ip As String)
    socket1.Initialize("socket1")
    socket1.Connect (Ip, port, 30000)
    WifiStatus = "Trying to connect..."
    UpdateUI
End Sub



Private Sub socket1_Connected (Successful As Boolean)
    'client connected to server
    If Successful Then
        WifiConnected = True
        StartAStream(socket1.InputStream, socket1.OutputStream)
        WifiStatus = "Connected"
    Else
        WifiStatus = "Error: " & LastException
    End If
    UpdateUI
End Sub

Private Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    'server accepted client
    If Successful Then
        WifiConnected = True
        StartAStream(NewSocket.InputStream, NewSocket.OutputStream)
        WifiStatus = "Connected"
    Else
        WifiStatus = "Error: " & LastException
    End If
    UpdateUI
    server.Listen
End Sub

Private Sub serial1_Connected (Success As Boolean)
    If Success Then
        BTStatus = "Connected"
        BTConnected = True
        StartAStream(serial1.InputStream, serial1.OutputStream)
    Else
        BTStatus = "Error: " & LastException.Message
    End If
    UpdateUI
End Sub

Private Sub StartAStream (In As InputStream, out As OutputStream)
    Log("StartAStream")
    astream.Initialize(In,  out, "astream")
    'call StartPlaying so the audio streamer will be ready to receive audio data
    audioStream.StartPlaying
End Sub

Public Sub SendAudio
    audioStream.StartRecording
    sendingAudio = True
End Sub

Public Sub StopSendingAudio
    audioStream.StopRecording
    sendingAudio = False
End Sub


Sub AudioStream_RecordBuffer (data() As Byte)
    Dim Packet As UDPPacket
    'ToastMessageShow("incoming packet"&Data.Length, True)
    If sendingAudio Then
        astream.Write(data)
        Packet.Initialize(data, "192.168.1.11", 3030)
        UDPSocket1.Send(Packet)
    End If
End Sub

Sub astream_NewData (Buffer() As Byte)
    If sendingAudio = False Then
        'play the received audio data
        audioStream.Write(Buffer)
    End If
End Sub


Sub Astream_Error
    Log("Error: " & LastException)
    astream.Close
    AStream_Terminated 'manually call this method as it will not be called
    'when we explicitly close the connection.
End Sub

Sub AStream_Terminated
    If BTConnected Then
        BTStatus = "Disconnected"
    Else If WifiConnected Then
        WifiStatus = "Disconnected"
    End If
    BTConnected = False
    WifiConnected = False
    audioStream.StopPlaying
    audioStream.StopRecording
    UpdateUI
End Sub

Sub Service_Destroy

End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
   
   
    udpdata = Packet.data
   
    ' ToastMessageShow("incoming packet"&Packet.Length, True)
    audioStream.Write(udpdata)
End Sub
 

Sahaya Arul

Member
Licensed User
Thankyou very much Philip...Everything is working well with one device connected,but when multicasted to host(192.168.43.255) so that all devices might hear,It is not working..What should I do...
 

trueboss323

Active Member
Licensed User
Not sure that I understand the question...
I mean like advanced features that we could try adding ourselves to the app. Maybe something like a group walkie talkie, where if everyone joins on the same wifi network, they could all talk as a group. Other features would include difference communication channels, voice modification, echo canceler, automatic gain control, noise suppressor, etc.
 

tigrot

Well-Known Member
Licensed User
I have a complete solution, working on public and local network. The compression is GSM.
The PC side is written in Microsoft NET. The PC side can be driven via a web based ACD. The call is initiated from the smartphone to void IP problems, but can easily piloted from the PC side. The process works in Webconnection environment as well.
 

luke2012

Well-Known Member
Licensed User
@Erel if I understood well, this is a point to point audio streaming, so this library is not suitable to implement a broadcast audio streaming, right?
 

Erel

Administrator
Staff member
Licensed User
Assuming that you are using it on a local network then you can switch to UDP and send the data to the broadcasting address.
 

Erel

Administrator
Staff member
Licensed User
Technically yes. The network communication will be more challenging. You can use MQTT to send short audio messages to all clients instead of streaming the audio.
 

imbault

Well-Known Member
Licensed User
I have a complete solution, working on public and local network. The compression is GSM.
The PC side is written in Microsoft NET. The PC side can be driven via a web based ACD. The call is initiated from the smartphone to void IP problems, but can easily piloted from the PC side. The process works in Webconnection environment as well.
Hi @tigrot, is it possible for you to share your solution, or by donation or buy it?

Thanks
Patrick
 

frasc

Member
Licensed User
Hypothetical question:

I know I can't connect 2 phones via cellular network because of port restrictions. If I had a B4J app running on a desktop top that acted as a proxy to accept incoming connections from both clients and simply passed along the data to both ends, would that work? or does the cellular network block both inbound and outbound connections on nonstandard ports?
 

Erel

Administrator
Staff member
Licensed User
It will work. You can use a WebSocket server for this. You will need to encode the data with base64 encoding.
 
Status
Not open for further replies.
Top