Receive audio from ProtectBabyMonitor, discarding it

When connected to a ProtectBabyMonitor service, accept
audio over the network. However, for now, discard the audio
instead of playing it.
This commit is contained in:
Branden Archer
2015-12-26 15:42:32 -05:00
parent 00049d4b2f
commit 936410e851

View File

@@ -16,8 +16,14 @@
*/ */
package protect.babymonitor; package protect.babymonitor;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ListenActivity extends Activity public class ListenActivity extends Activity
{ {
@@ -27,6 +33,22 @@ public class ListenActivity extends Activity
int _port; int _port;
String _name; String _name;
Thread _listenThread;
private void streamAudio(Socket socket) throws IllegalArgumentException, IllegalStateException, IOException
{
Log.i(TAG, "Setting up stream");
InputStream is = socket.getInputStream();
int read = 0;
int bufferSize = 1024; // set this to an appropriate value.
while(socket.isConnected() && read != -1 && Thread.currentThread().isInterrupted() == false)
{
byte [] buffer = new byte[bufferSize*2];
read = is.read(buffer);
}
socket.close();
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
{ {
@@ -38,11 +60,47 @@ public class ListenActivity extends Activity
_name = b.getString("name"); _name = b.getString("name");
setContentView(R.layout.activity_listen); setContentView(R.layout.activity_listen);
_listenThread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Socket socket = new Socket(_address, _port);
streamAudio(socket);
}
catch (UnknownHostException e)
{
Log.e(TAG, "Failed to stream audio", e);
}
catch (IOException e)
{
Log.e(TAG, "Failed to stream audio", e);
}
ListenActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
final TextView statusText = (TextView) findViewById(R.id.textStatus);
statusText.setText("Listening stopped");
}
});
}
});
_listenThread.start();
} }
@Override @Override
public void onDestroy() public void onDestroy()
{ {
_listenThread.interrupt();
_listenThread = null;
super.onDestroy(); super.onDestroy();
} }
} }