用 Java Sound API 播放 PCM 音頻 | Playing PCM Audio with Java Sound API |
多媒體應用程序,比如因特網電話,需要實時播放音頻,于是就要處理內存中的音頻數據。本文將演示如何播放 PCM(脈沖編碼調制)音頻。 | Multimedia application, such as an Internet phone, requires playing sound in real time. Thus we are going to deal with in-memery audio data. This article will show how to play PCM (Pulse Code Modulation) audio. |
假設有一個字節數組 audioBuffer 用來保存將要播放的音頻塊。做以下的初始化: | Suppose there is a byte array audioBuffer which holds a chunck of audio data to be played. Do the following initializations: |
|
|
接著只要不停地用 sdl 向混頻器寫入字節: | Then just continously write bytes to mixer with sdl: |
|
|
因為 audioBuffer 被不斷填充和播放,所以最好把上述代碼放入線程。播放結束后像這樣來停止 sdl: | Since audioBuffer is filled and played on and on, it's best to put the above code in a thread. When the playing is over, stop sdl like this: |
|