我目前正在嘗試將直播麥克風(fēng)音頻從Android設(shè)備流式傳輸?shù)絁ava程序。我首先在兩個(gè)android設(shè)備之間發(fā)送實(shí)時(shí)音頻,以確認(rèn)我的方法正確。在接收設(shè)備上幾乎沒(méi)有任何延遲地可以完美地聽(tīng)到音頻。接下來(lái),我將相同的音頻流發(fā)送到一個(gè)小型Java程序,并驗(yàn)證了數(shù)據(jù)也已正確發(fā)送到此處。現(xiàn)在,我想要做的是對(duì)這些數(shù)據(jù)進(jìn)行編碼,并以某種方式在運(yùn)行Java程序的服務(wù)器上對(duì)其進(jìn)行回放。我寧愿在使用HTML5或JavaScript的網(wǎng)絡(luò)瀏覽器中播放它,但可以使用VLC等替代方法。這是發(fā)送實(shí)時(shí)麥克風(fēng)音頻的Android應(yīng)用的代碼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;@Overrideprotected 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;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true;}這是Java程序讀取數(shù)據(jù)的代碼。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); } }}我知道在將音頻發(fā)送到Java程序之前,應(yīng)該先在應(yīng)用程序端對(duì)音頻進(jìn)行編碼,但是我不確定在使用AudioRecorder時(shí)如何進(jìn)行編碼。我寧愿不使用NDK,因?yàn)槲覜](méi)有使用它的經(jīng)驗(yàn),也沒(méi)有足夠的時(shí)間學(xué)習(xí)如何使用它。...:)
將實(shí)時(shí)Android音頻流傳輸?shù)椒?wù)器
喵喵時(shí)光機(jī)
2020-02-04 15:38:39