Android Tutorial Walkie Talkie - Audio streaming over Wifi or Bluetooth

Discussion in 'Tutorials & Examples' started by Erel, Jun 30, 2013.

Thread Status:
Not open for further replies.
  1. Sahaya Arul

    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...
     
  2. Philip Prins

    Philip Prins Active Member Licensed User

    Hello Sahaya,

    You can send a UDP stream multicast to x.x.x.255 ,
     
    Sahaya Arul likes this.
  3. Sahaya Arul

    Sahaya Arul Member Licensed User

    @ philip: Can you update the walkie talkie to an UDP Version and send it over to me...
     
    Last edited: Sep 8, 2015
  4. Philip Prins

    Philip Prins Active Member Licensed User

    Code:
    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"8000True16, 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 StringIntent 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
     
  5. Sahaya Arul

    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...
     
  6. trueboss323

    trueboss323 Active Member Licensed User

    Hi Erel, what are some advanced features we can try adding to this sample walkie talkie?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that I understand the question...
     
  8. trueboss323

    trueboss323 Active Member Licensed User

    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.
     
  9. tigrot

    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.
     
  10. luke2012

    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?
     
  11. Erel

    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.
     
  12. trueboss323

    trueboss323 Active Member Licensed User

    Erel is it possible to have a multi-channel / group walkie talkie function?
     
  13. Erel

    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.
     
  14. trueboss323

    trueboss323 Active Member Licensed User

    Can this also work for Bluetooth too ?
     
  15. Philip Prins

    Philip Prins Active Member Licensed User

    Not sure, i did not test it on BT
     
  16. xpectmore

    xpectmore Member Licensed User

  17. imbault

    imbault Well-Known Member Licensed User

    Hi @tigrot, is it possible for you to share your solution, or by donation or buy it?

    Thanks
    Patrick
     
  18. frasc

    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?
     
  19. Erel

    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.
     
  20. trueboss323

    trueboss323 Active Member Licensed User

    How can i make it so you can transmit audio using the volume buttons?
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice