我正在嘗試將Android設備上的現場麥克風音頻傳輸到Java程序。我開始在兩個Android設備之間發送實時音頻以確認我的方法是正確的。可以在接收設備上幾乎沒有任何延遲的情況下完美地聽到音頻。接下來,我將相同的音頻流發送到一個小型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程序讀取的代碼爲Android應用程序的代碼..
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,因爲我沒有經驗與它沒有真正的時間來學習如何使用它......但:) :)
你使用流音頻是什麼?我只想要一種方法。 – Atieh 2014-12-05 16:28:45
我有興趣編寫一個Android應用程序,將Android設備的實時麥克風音頻流式傳輸到桌面應用程序。您能否提供一些有用的指向一些與您發佈的內容相關的資源?這將非常有幫助!謝謝! :) – wayway 2016-02-07 04:45:51
你如何做兩個Android設備之間的現場音頻? – pkBhati 2016-02-25 07:08:38