Android Question PC to Android Audio streaming

Ian Garton

Member
Licensed User
Longtime User
Erel's Walkie-Talkie example is a very nice simple example for Android <--> Android voice.
I'm trying to implement a PC <--> Android version with the same Android application Erel posted.

Using the NAudio library on the PC seems to be the simplest way to do this, let me know if you think otherwise.
For starters I'm just trying to get PC --> Android direction audio working, but am having the android application crash out after a second with an Out Of Memory exception every time the PC application starts streaming bytes to it, within about a second.

B4X:
java.lang.OutOfMemoryError
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:172)
    at java.lang.Thread.run(Thread.java:856)

Below is my PC side VB.net code which is performing the socket connection and audio streaming.

B4X:
Public Class Form1

    Dim WithEvents AudioIn As New NAudio.Wave.WaveIn()
    Dim SocketOut As New Net.Sockets.TcpClient
    Dim NetworkStreamOut As Net.Sockets.NetworkStream

    Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click

        AudioIn.DeviceNumber = 0   ' PC Microphone
        AudioIn.WaveFormat = New NAudio.Wave.WaveFormat(22050, 16, 1)   ' Sample Rate, Bits per Sample, Channels

        SocketOut.Connect("192.168.0.14", 21341)    ' Android Device IP
        Do While Not SocketOut.Connected
            Application.DoEvents()
        Loop

        AudioIn.StartRecording()
        NetworkStreamOut = SocketOut.GetStream

    End Sub


    Private Sub AudioIn_DataAvailable(sender As Object, e As NAudio.Wave.WaveInEventArgs) Handles AudioIn.DataAvailable

        If e.Buffer.Length > 0 Then
            NetworkStreamOut.Write(e.Buffer, 0, e.Buffer.Length)
            NetworkStreamOut.Flush()
        End If

    End Sub

End Class

Any thoughts or ideas most welcome!
 

Ian Garton

Member
Licensed User
Longtime User
Many thanks Erel for the information.
I just needed to change the astream.Initialise line in the Connector service, and it works perfectly.
 
Upvote 0

Nizze

Active Member
Licensed User
Longtime User
Many thanks Erel for the information.
I just needed to change the astream.Initialise line in the Connector service, and it works perfectly.
Hi .

I have been sitting trying to make a server side in VB.net
I have been looking at Ian's examples but i can not figure out how NAudio works

I can see how we start and make contact with Andoid client .
And that works perfect
And i can disconnect as well

But i can not reconnect , then NAudio crashes ( I think )

And i would also like to send audio from Android --> to pc .
Any help would be more then helpful ..

B4X:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        AudioIn.DeviceNumber = 0  ' PC Microphone
        AudioIn.WaveFormat = New NAudio.Wave.WaveFormat(22050, 16, 1)  ' Sample Rate, Bits per Sample, Channels
        AudioOut.DeviceNumber = 0

        ' Android Device IP
        If Not SocketOut.Connected Then
            SocketOut.Connect("192.168.0.125", 21341)
            AudioIn.StartRecording()
            NetworkStreamOut = SocketOut.GetStream
        Else
            AudioIn.StartRecording()
        End If

        Do While Not SocketOut.Connected
            Application.DoEvents()
        Loop

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        AudioIn.StopRecording()


    End Sub

Br
Nizze
 
Last edited:
Upvote 0
Top