Android Tutorial Walkie Talkie - Audio streaming over Wifi or Bluetooth

Status
Not open for further replies.
This example implements a simple "walkie talkie".

270px-Wikipedia_images_011.jpg

(src: wikipedia)

SS-2013-06-30_16.26.21.png


Once the two devices are connected, either over Bluetooth or over the local network, you can press on the activity and talk. The audio captured from the microphone will be sent to the other device and played. Note that this is a half duplex solution, which means that audio will not be received while you send audio. This is to avoid positive feedback (which will also occur if the devices are close to each other).

Most of the code handles the connection. Streaming the audio is done with AudioStreamer object which was introduced in Audio library v1.5.

Note that if you are only interested in audio recording then you should follow the two examples in the above link.

The "interesting" code that is responsible for capturing the audio and sending it is quite simple:
B4X:
Sub AudioStream_RecordBuffer (Data() As Byte)
   If sendingAudio Then
      astream.Write(Data)
   End If
End Sub
Then in the other side:
B4X:
Sub astream_NewData (Buffer() As Byte)
   If sendingAudio = False Then
      'play the received audio data
      audioStream.Write(Buffer)
   End If
End Sub
 

Attachments

  • Walkie-Talkie.zip
    10.7 KB · Views: 11,148

Sahaya Arul

Member
Licensed User
Longtime User
@ philip: Can you update the walkie talkie to an UDP Version and send it over to me...
 
Last edited:

Philip Prins

Active Member
Licensed User
Longtime 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
Longtime 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
Longtime 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
Longtime 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
Longtime 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?
 

imbault

Well-Known Member
Licensed User
Longtime 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?
 

ciapw

Member
Licensed User
Hi.. can I use this example to implement voice communication between my android phone and nanopi neo 2 (there will be microphone and loudspeaker attached on the nanopi neo 2 as well) ? So both of the phone and nanopi neo 2 can both receive and transmit voice messages?
If it couldn't . How to implement this?
Thank you. I am looking forward to your comments.
 
Status
Not open for further replies.
Top