I want to have a functionality of real time audio streaming on android device which is capturing audio through the MIC of the device and send it to the server. I know to send a send a audio file after recording but in case of real time I need help. May be it can be done by sending byte array continually to the server. If so how or if any other way, Please share your ideas. Thanks.
EDIT-
Android Client Code:-
public class Main extends Activity {
private MediaRecorder recorder;
private final String TAG = "AudioTest";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String hostname = "192.168.50.25";
int port = 2004;
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(hostname), port);
} catch (UnknownHostException e) {
Log.d(TAG, "Inside UnknownHostException@@@@@@@@@@@@@@@@@@@@@@");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "Inside IOException%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
e.printStackTrace();
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());
try {
Log.i(TAG, pfd.getFileDescriptor().toString());
} catch (Exception e) {
Log.d(TAG, "Inside MyException################################");
}
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
JAVA Server Code-
public class Provider {
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider() {
}
void run() {
try {
// 1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
// 2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from "
+ connection.getInetAddress().getHostName());
// 3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
// 4. The two parts communicate via the input and output streams
do {
try {
message = (String) in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
sendMessage("bye");
} catch (ClassNotFoundException classnot) {
System.err.println("Data received in unknown format");
}
} while (!message.equals("bye"));
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
// 4: Closing connection
try {
in.close();
out.close();
providerSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
void sendMessage(String msg) {
try {
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static void main(String args[]) {
Provider server = new Provider();
while (true) {
server.run();
}
}
}
You can use sockets as so:
String hostname = "1.2.3.4";
int port = 865;
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(hostname), port);
} catch (Exception e) {
e.printStackTrace();
}
ParcelFileDescriptor socketedFile = ParcelFileDescriptor.fromSocket(socket);
Then set the socketedFile to the output file (socketedFile.getFileDescriptor()) of the audio recorder. This will send it up as bytes.
Alternatively to make it more stable, write out the data from the MediaRecorder to a local buffer and then have a separate thread check that buffer and write it to the socket instead, to allow for small disconnections in the socket connection.
See this question for more information: android stream audio to server
Obviously you then need to have an application running on a server to receive your bytes and turn that into audio data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With