神奇好望角 The Magical Cape of Good Hope

          庸人不必自擾,智者何需千慮?
          posts - 26, comments - 50, trackbacks - 0, articles - 11
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          用 Java Sound API 播放 PCM 音頻

          Posted on 2007-11-27 23:06 蜀山兆孨龘 閱讀(4480) 評論(2)  編輯  收藏
          用 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:
          1. AudioFormat af = new AudioFormat(
          2.         sampleRate, sampleSizeInBits, channels, signed, bigEndian);
          3. SourceDataLine.Info info = new DataLine.Info(
          4.         SourceDataLine.class, af, bufferSize);
          5. SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info);
          6. sdl.open(af);
          7. sdl.start();
            接著只要不停地用 sdl 向混頻器寫入字節:     Then just continously write bytes to mixer with sdl:
          1. int offset = 0;
          2. while (offset < audioData.length) {
          3.     offset += sdl.write(audioData, offset, bufferSize);
          4. }
            因為 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:
          1. sdl.drain();
          2. sdl.stop();

          評論

          # re: 用 Java Sound API 播放 PCM 音頻  回復  更多評論   

          2013-08-22 17:01 by Stream
          就這樣?

          這么簡潔?

          沒看明白?


          我用android下的 audioRecord通過socket傳到pc機上,可以用這個播放???

          audioRecord傳的是pcm的.


          能詳細點么?

          qq:147654368 求指教啊

          # re: 用 Java Sound API 播放 PCM 音頻  回復  更多評論   

          2013-08-22 22:12 by Stream
          在不斷的嘗試下,我終于成功了,恩,LS的還是我


          LZ為什么不解釋下那幾個參數呢?
          搞我不斷的換來試......


          ps. 自己不會查API呀-__-

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 皮山县| 郎溪县| 青岛市| 土默特左旗| 天气| 乌兰浩特市| 衡阳县| 惠州市| 遂宁市| 连平县| 南雄市| 榆树市| 襄汾县| 绥江县| 中山市| 巴林右旗| 曲沃县| 黄石市| 渭南市| 安庆市| 英吉沙县| 福州市| 济南市| 彭阳县| 长白| 嵊泗县| 呈贡县| 泽普县| 乌兰察布市| 玛纳斯县| 梅河口市| 阜南县| 麻城市| 新建县| 福建省| 平潭县| 高碑店市| 巴里| 都兰县| 鲁甸县| 兴国县|