6
我想要在Android設備上實現實時音頻流功能,該設備通過設備的MIC捕獲音頻並將其發送到服務器。我知道錄音後發送一個音頻文件,但在實時情況下,我需要幫助。可以通過將字節數組連續發送到服務器來完成。如果是這樣,或者如果有其他方式,請分享您的想法。謝謝。流媒體實時音頻
編輯 -
Android客戶端代碼: -
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 [email protected]@@@@@@@@@@@@@@@@@@@@@");
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服務器代碼 -
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();
}
}
}
嗨米奇...非常感謝您的關注。但我得到另一個異常(java.io.StreamCorruptedException:無效的流頭:00000018)。如果可以,請在這方面提供幫助。參考編輯的問題。 – Nitin4Android 2012-03-21 11:25:12
+1給你...... – Nitin4Android 2012-03-21 11:36:05