Receive Live Audio Data Via Socket on Android - java

I have coded an app to connect to a network socket on an Rpi over WiFi and have successfully sent data, such as a string, to and from the client and server. The next step is to have the client, my phone in this case, receive and play audio data in real time that is being sent from the server.
So far I have socket and audio variables declared. Once a button is hit, an async task "Listen" is started. I know it can connect to the socket. After that, I'm trying to open an input stream and feed it into a buffer, and then have that buffer played in real time.
I think I'm close to figuring it out, but I don't think I have it quite right yet. I have marked areas I am unsure of with TODO in my code.
public class MainActivity extends AppCompatActivity {
//audio variables
static final int sampleFreq = 44100;
static final int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
static final int streamType = STREAM_MUSIC;
static final int audioMode = MODE_STREAM;
static final int bufferSize = getMinBufferSize(sampleFreq, channelConfig, audioMode);
//socket variables
public Socket mySocket;
private int SERVERPORT = 33333;
private static final String SERVER_IP = "172.24.1.1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
}
public void onClickListen(View view) {
//call async task
new Listen().execute();
}
private class Listen extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
//get IP and port number
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Log.d(debugStr, "In initial listen connect");
//create socket
mySocket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (mySocket == null) {
str1 = "Socket became null, listener";
return null;
}
//TODO here is all the new audio stuff
try {
//creates buffer to hold incoming audio data
byte [] audioBuffer = new byte[4096];
//creates input stream readers to read incoming data
BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
DataInputStream myDis = new DataInputStream(myBis);
Log.d(debugStr, "Input created, listener");
// Read the file into the music array.
int i = 0;
//TODO unsure of while loop condition
while (myDis.read() != -1) {
//since incoming audio is unknown size, use estimated buffer size
audioBuffer[bufferSize-1-i] = myDis.readByte();
i++;
}
//create audio track object
AudioTrack myAudioTrack = new AudioTrack(streamType, sampleFreq, channelConfig, audioEncoding, bufferSize, audioMode);
//TODO write audio data to somewhere?
//TODO should this be in a while loop for live stream?
//TODO will this actually play the audio?
myAudioTrack.write(audioBuffer, 0, bufferSize);
//close input streams
myDis.close();
myBis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
mySocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
}
}
}
I don't think my while loop condition is correct, or if that is even the correct way to read data from an input stream. I also think that the audioTrack.write should be in the while loop, if that does actually make audio play.
Any help or clarification is greatly appreciated!

I figured it out.
byte [] audioBuffer = new byte[4096];
//creates input stream readers to read incoming data
BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
DataInputStream myDis = new DataInputStream(myBis);
Log.d(debugStr, "Input created, listener");
AudioTrack myAudioTrack = new AudioTrack(streamType, sampleFreq, channelConfig, audioEncoding, bufferSize, audioMode);
//Log.d(debugStr, String.valueOf(mySocket.getInputStream().read(audioBuffer)));
Log.d(debugStr, "track made");
// Read the file into the music array.
int i = 0;
//TODO unsure of while loop condition
while (mySocket.getInputStream().read(audioBuffer) != -1) {
audioBuffer[audioBuffer.length-1-i] = myDis.readByte();
myAudioTrack.play();
myAudioTrack.write(audioBuffer, 0, audioBuffer.length);
i++;
}
//close input streams
myDis.close();
myBis.close();

Related

Send messages to Handler from other activity

I Have a code that connects to a bluetooth device, opens a bluetooth socket that communicates with a running thread which operates functions running in main activity.
I would like to move all the connecting sequence to another activity, and then operate the thread from the main one as done now. The problem is they are all connected.
I would like to have the option of sending a message between these activities(meaning remaining the socket operating from the other activity), i.e this message:
mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
.sendToTarget();
because it is impossible to pass handler between activities I don't know how/if possible to do so.
What is the best way of doing such a thing?
added part of the code.
Thanks.
mHandler = new Handler(){
public void handleMessage(android.os.Message msg){
if(msg.what == MESSAGE_READ){
String readMessage = null;
try {
readMessage = new String((byte[]) msg.obj, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
RxMessage = readMessage.split(" ");
if (sH.isStringInCorrectOrder(RxMessage,Weight))
populateListView(RxMessage);
mReadBuffer.setText(readMessage);
}
if(msg.what == CONNECTING_STATUS){
if(msg.arg1 == 1)
mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
else
mBluetoothStatus.setText("Connection Failed");
}
}
};
private void connectBT (){
mBluetoothStatus.setText("Connecting...");
// Get the device MAC address, which is the last 17 chars in the View
final String address = "98:D3:31:30:39:75";
final String name = "HC-06";
// Spawn a new thread to avoid blocking the GUI one
new Thread()
{
public void run() {
boolean fail = false;
BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
try {
mBTSocket = createBluetoothSocket(device);
} catch (IOException e) {
fail = true;
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
}
// Establish the Bluetooth socket connection.
try {
mBTSocket.connect();
} catch (IOException e) {
try {
fail = true;
mBTSocket.close();
mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
.sendToTarget();
} catch (IOException e2) {
//insert code to deal with this
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
}
}
if(fail == false) {
mConnectedThread = new ConnectedThread(mBTSocket);
mConnectedThread.start();
mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
.sendToTarget();
}
}
}.start();
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.available();
if(bytes != 0) {
SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
bytes = mmInStream.available(); // how many bytes are ready to be read?
bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget(); // Send the obtained bytes to the UI activity
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(String input) {
byte[] bytes = input.getBytes(); //converts entered String into bytes
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
Just declare mHandler as static and you can access it from all other activities. This will create a small temporary memory leak, but don't worry about it.

Input Stream Issue

stuck in One Issue ,
I am using BluetoothSocket class, I am sending and receiving data with the help of input and output streams.
when App receives large amount of data from input stream, I am killing my app forcefully and after it I am again restarting my app, but InputStream returns me previous data, which is not needed anymore.how to discard that old data?
has Anyone Some Solution for this Issue?
Following is my source code:
public class MyBluetoothService {
private static final String TAG = "MY_APP_DEBUG_TAG";
private Handler mHandler; // handler that gets info from Bluetooth service
// Defines several constants used when transmitting messages between the
// service and the UI.
private interface MessageConstants {
public static final int MESSAGE_READ = 0;
public static final int MESSAGE_WRITE = 1;
public static final int MESSAGE_TOAST = 2;
// ... (Add other message types here as needed.)
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private byte[] mmBuffer; // mmBuffer store for the stream
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams; using temp objects because
// member streams are final.
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
Log.e(TAG, "Error occurred when creating input stream", e);
}
try {
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "Error occurred when creating output stream", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
mmBuffer = new byte[1024];
int numBytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs.
while (true) {
try {
// Read from the InputStream.
numBytes = mmInStream.read(mmBuffer);
// Send the obtained bytes to the UI activity.
Message readMsg = mHandler.obtainMessage(
MessageConstants.MESSAGE_READ, numBytes, -1,
mmBuffer);
readMsg.sendToTarget();
} catch (IOException e) {
Log.d(TAG, "Input stream was disconnected", e);
break;
}
}
}
// Call this from the main activity to send data to the remote device.
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
// Share the sent message with the UI activity.
Message writtenMsg = mHandler.obtainMessage(
MessageConstants.MESSAGE_WRITE, -1, -1, mmBuffer);
writtenMsg.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Error occurred when sending data", e);
// Send a failure message back to the activity.
Message writeErrorMsg =
mHandler.obtainMessage(MessageConstants.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString("toast",
"Couldn't send data to the other device");
writeErrorMsg.setData(bundle);
mHandler.sendMessage(writeErrorMsg);
}
}
// Call this method from the main activity to shut down the connection.
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the connect socket", e);
}
}
}
}
// Keep listening to the InputStream until an exception occurs
The problem is here. You should keep reading from the stream until end of stream or an exception occurs. You need to break out of the read loop if read() returns -1.
At present you are reading beyond end of stream, and ignoring the condition altogether, so of course the data that was in the buffer on the last successful read is still there.
For your application to keep seeing that data, you must also be ignoring the read count and assuming the buffer was filled, which also is invalid.
I think you should close the socket to manage the bug.
I recommend you to do this in finalizer like the code below.
private class ConnectedThread extends Thread
{
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private byte[] mmBuffer; // mmBuffer store for the stream
#override
protected void finalize() throws Throwable
{
try
{
cancel();
}
finally
{
super.finalize();
}
}
...
Also as I mentioned in comment, it is safer to close every streams before closing the socket.
So, try this cancel() method.
// Call this method from the main activity to shut down the connection.
public void cancel()
{
try {
mmInStream.close();
} catch( NullPointerException | IOException e) {}
try {
mmOutStream.close();
} catch( NullPointerException | IOException e) {}
try
{
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the connect socket", e);
}
}
And more information about finalize method.
EDIT: bold ones are important than other suggestions.
Reading comments of EJP I understood why your app stops when you get large data : you maybe have to clear buffers before calling read(). And he says that finalizer can happen not to be called by system (I don't know why).
How about breaking the loop when the read() returned -1?
And just now I found a helpful link about proper method to read a stream. I hope it helped.
Code cited from the link
private String extract(InputStream inputStream) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, read);
}
baos.flush();
return new String(baos.toByteArray(), "UTF-8");
}
Also
Though the finalizer may be able not to be called by system closing streams before closing sockets is safer (I read some SO threads before).

Stream Audio from Client to Server to Multiple Clients Java

As the title suggests I'm making an application that Streams audio from a client to a server where I store the audio and then distribute it to multiple clients to be played. I've got everything working up until the audio storage, but I can't seem to stream the audio to multiple clients.
Here is my attempt:
Server Code:
class Server {
static int port = 50005;
static int listen = 50010;
static int listenerPort = 50015;
static DatagramSocket serverSocket, listenSocket,
broadcastSocket;
static byte[] receiveData, listenData;
static DatagramPacket receivePacket, listenPacket;
static DataOutputStream out;
static ArrayList<String> listeners = new ArrayList<String>();
static File file = new File("recording.bin");
static boolean active = true;
public static void main(String args[]) throws Exception {
//Define the Receiving Datagram Socket
serverSocket = new DatagramSocket(port);
//Define the Timeout of the socket
serverSocket.setSoTimeout(10000);
//Define the listening socket
listenSocket = new DatagramSocket(listen);
listenSocket.setSoTimeout(10000);
//Define Broadcasting socket
broadcastSocket = new DatagramSocket();
//Define data size, 1400 is best sound rate so far
receiveData = new byte[1400];
listenData = new byte[256];
//Define the DatagramPacket object
receivePacket = new DatagramPacket(receiveData, receiveData.length);
listenPacket = new DatagramPacket(listenData, listenData.length);
//Prepare the DataOutputStream to write to file.
out = new DataOutputStream(new FileOutputStream(file));
//Write and Broadcast on a separate thread
Thread t = new Thread() {
#Override public void run() {
getPackets();
}
};
t.start();
//Set up Connection Listener on a separate thread
Thread l = new Thread() {
#Override public void run() {
listen();
}
};
l.start();
}
/***
* Function that gets the audio data packets
* saves them, and outputs the audio to the speakers.
*/
public static void getPackets() {
while (active) {
try {
//Wait until packet is received
serverSocket.receive(receivePacket);
System.out.println("Receiving Data");
//Write to Binary file
out.write(receiveData, 0, receiveData.length);
//Send data
sendData(receivePacket.getData());
} catch (IOException e) {
active = false;
//If connection times out close it
try {
out.close();
} catch (IOException t) {
//Do nothing
}
System.out.println("Converting to audio");
//Convert audio file
new Convert().toWAV();
}
}
}
/***
* Function that listens if there are any connections made to
* the listener port and creates a datagram socket to stream audio
* to anyone who connects
*/
public static void listen() {
while (active) {
try {
//Wait until packet is received
listenSocket.receive(listenPacket);
listeners.add(listenPacket.getAddress().getHostAddress());
System.out.println("Client received");
} catch (IOException e) {
if(active) {
listen();
}
}
}
}
public static void sendData(byte[] data) {
try {
for (int i = 0; i < listeners.size(); i++) {
InetAddress destination = InetAddress.getByName(listeners.get(i));
broadcastSocket.send(new DatagramPacket(data, data.length, destination, listenerPort));
System.out.println("Sending Data");
}
} catch (Exception e) {
//If it failed to send don't do anything
e.printStackTrace();
}
}
}
And here is the code I run on the multiple clients:
class Receiver {
static AudioInputStream ais;
static AudioFormat format;
static boolean active = true;
static int port = 50015;
static DatagramSocket serverSocket, socket;
static byte[] receiveData;
static DatagramPacket receivePacket, packet;
static ByteArrayInputStream bais;
static int sampleRate = 8000;
static int time = 10;
static DataLine.Info dataLineInfo;
static SourceDataLine sourceDataLine;
public static void main(String args[]) throws Exception {
socket = new DatagramSocket();
InetAddress destination = InetAddress.getByName("server ip address");
byte[] temp = new byte[256];
//putting buffer in the packet
packet = new DatagramPacket(temp, temp.length, destination, 50010);
socket.send(packet);
//Define the Receiving Datagram Socket
serverSocket = new DatagramSocket(port);
//Define data size, 1400 is best sound rate so far
receiveData = new byte[1400];
//Define the format sampleRate, Sample Size in Bits, Channels (Mono), Signed, Big Endian
format = new AudioFormat(sampleRate, 16, 1, true, false);
//Define the DatagramPacket object
receivePacket = new DatagramPacket(receiveData, receiveData.length);
//Prepare the Byte Array Input Stream
bais = new ByteArrayInputStream(receivePacket.getData());
//Now concert the Byte Array into an Audio Input Stream
ais = new AudioInputStream(bais, format, receivePacket.getLength());
//Define DataLineInfo
dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
//Get the current Audio Line from the system
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
//Open up sourceDataLine and Start it
sourceDataLine.open(format);
sourceDataLine.start();
//Write and play on a separate thread
Thread t = new Thread() {
#Override
public void run() {
getPackets();
}
};
t.start();
//Now keep track of time
while (time > 0) {
time--;
Thread.sleep(1000);
if (time == 0) {
active = false;
}
}
//Close SourceDataLine
sourceDataLine.drain();
sourceDataLine.close();
}
/***
* Function that gets the audio data packets
* saves them, and outputs the audio to the speakers.
*/
public static void getPackets() {
try {
while (active) {
System.out.println("Receiving");
//Wait until packet is received
serverSocket.receive(receivePacket);
//Reset time
time = 10;
//Send data to speakers
toSpeaker(receivePacket.getData());
}
} catch (IOException e) {
}
}
/***
* Function that plays the sound bytes with the speakers.
* #param soundbytes = bytes of sound sent to speakers
*/
public static void toSpeaker(byte soundbytes[]) {
try {
sourceDataLine.write(soundbytes, 0, soundbytes.length);
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}
}
I've verified that the server does receive the initial connection which I use to get the clients ip address, but the client does not seem to receive any data, and I'm getting no errors on run time.
Any help would be much appreciated.
Ok so after messing around with this for a while I've realized the problem was not the code, but rather that the admins of the building's network where blocking me. So I'm going to go ahead and mark this as answered.

Bluetooth data transfer between two Android devices

I have been following this Android guide for Bluetooth communication
To explain exactly what I want to do, when the two devices are paired, two different activities open up on each device (server and client) where on the server activity I have different buttons, and on the client activity there is just a textview.
I want to be able to press a button on the server device and display it on the client.
I have managed to establish a connection between the two devices, but now I want to send data which I have not been able to do.
They give this code for data transfer:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
But this line generates an error
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
And is not explained in the guide. I don't know what the mHandler is or does.
Apart from the error, I don't even really understand where to put this code. Should it be in the second activities (server and client) that I open or in the main? If in the Server activity, should it be in the onClick method for all the buttons with a different byte code to send for each button? And in this code, how do we distinguish who is sending and who is receiving?
Check out the BluetoothChat example that Google provides in the SDK. It'll show you how to implement basic sending of text over bluetooth.
You can also try the tutorial example here
Can you please describe the error as seen by you?
As informed by Ankit and Addy, BlueToothChat is the best code for you to refer. Conduct an experiment by loading it on 2 android devices - use one as server other as client to exchange the messages between them. Such experiment will help you to understand it's code and decide your coding logic.
mHandler is used for passing message from your BluetoothHandle.java to your Activity. This will help you to update messages on your screen which are returned by BluetoothHandler.
you have to create mHandler from your activity and call your handler like this -
mBluetoothHandler = new BluetoothHandler(this, mHandler);
and your BluetoothHandler.java has constructor like this -
public class BluetoothHandler {
public BluetoothHandler(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
mcontext = context;
}
}
For more details, please refer Android sample project of Bluetooth Chat .
You can also use this link :
http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html
// Enter code here
Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
switch(msg.what) {
case 1:
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
break;
}
}
};

Anyone Have MediaRecorder Working with ParcelFileDescriptor.fromDatagramSocket?

I am trying to work out an example of recording audio with MediaRecorder, which recorded data is passed through UDP socket on Android(>=4.0).
Reading an article:
Broadcasting video with Android - without writing to local files,
I employed ParcelFileDescriptor.fromDatagramSocket
Here is a simplified pseudocode I'm currently working on:
//private DatagramSocket ds;
//private MediaRecorder mRecorder;
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(ds);
mRecorder = new MediaRecorder();
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOutputFile(pfd.getFileDescriptor());
mRecorder.prepare();
mRecorder.start();
Here is just a simple DatagramSocket(UDP) Listener thread loop to observe DatagramSocket ds
//...
private DatagramSocket ds;
private int localPort = 39000;
private MediaRecorder mRecorder;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
ds = new DatagramSocket(localPort);
}
catch (SocketException e)
{
System.out.println(e);
}
class IOThread extends Thread
{
#Override
public void run()
{
byte []buf = new byte[1024];
DatagramPacket packet= new DatagramPacket(buf, buf.length);
while (true)
{
try
{
ds.receive(packet);
SocketAddress sockAddress = packet.getSocketAddress();
int len = packet.getLength();
String msg = new String(buf, 0, len);
System.out.println(msg + ":" + len + "byte receive by "+ sockAddress.toString());
}
catch (IOException e)
{
System.out.println(e);
}
}
}
}
IOThread io1 = new IOThread();
io1.start();
//..........
In principle, I expect recorded buffer by MediaRecorder is passed through the UDP FileDescriptor, and somehow I can manage the socket, but so far, I observe nothing happening.
I confirmed the UDP listener unit is working fine with other UDP input, and MediaRecorder unit should be fine since it works with localFiles not UDP socket FileDescriptor.
Any suggestions?
Thanks in advance!
Probably, it's best to pass MediaRecorder -> LocalSocket,
and here is my subsequent Q&A
a LocalSocket (Unix domain) client-server data flow issue for MediaRecorder in Android (Java)

Categories

Resources