我目前正在尝试将实时麦克风音频从 Android 设备流式传输到 Java 程序。我开始在两个安卓设备之间发送实时音频,以确认我的方法是正确的。在接收设备上几乎没有任何延迟,可以完美地听到音频。接下来,我将相同的音频流发送到一个小型 Java 程序,并且我验证了数据也正确地发送到这里。现在我想做的是对这些数据进行编码,并以某种方式在运行 Java 程序的服务器上播放它。我宁愿在使用 HTML5 或 JavaScript 的网络浏览器中播放它,但我对 VLC 等替代方法持开放态度。
这是发送实时麦克风音频的 Android 应用程序的代码
public class MainActivity extends Activity {
private Button startButton,stopButton;
public byte[] buffer;
public static DatagramSocket socket;
AudioRecord recorder;
private int sampleRate = 44100;
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
private boolean status = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById (R.id.start_button);
stopButton = (Button) findViewById (R.id.stop_button);
startButton.setOnClickListener(startListener);
stopButton.setOnClickListener(stopListener);
minBufSize += 2048;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private final OnClickListener stopListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
status = false;
recorder.release();
Log.d("VS","Recorder released");
}
};
private final OnClickListener startListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
status = true;
startStreaming();
}
};
public void startStreaming()
{
Thread streamThread = new Thread(new Runnable(){
@Override
public void run()
{
try{
DatagramSocket socket = new DatagramSocket();
Log.d("VS", "Socket Created");
byte[] buffer = new byte[minBufSize];
Log.d("VS","Buffer created of size " + minBufSize);
Log.d("VS", "Address retrieved");
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VS", "Recorder initialized");
recorder.startRecording();
InetAddress IPAddress = InetAddress.getByName("192.168.1.5");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
while (status == true)
{
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 50005);
socket.send(sendPacket);
}
} catch(UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
Log.e("VS", "IOException");
e.printStackTrace();
}
}
});
streamThread.start();
}
}
这是Java程序读取数据的代码..
class Server
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(50005);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData().toString());
System.out.println("RECEIVED: " + sentence);
}
}
}
我知道在将音频发送到 Java 程序之前,我应该在应用程序端对音频进行编码,但我不确定在使用 AudioRecorder 时如何进行编码。我宁愿不使用 NDK,因为我没有使用它的经验,也没有时间学习如何使用它......但是 :)