神奇好望角 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 蜀山兆孨龘 閱讀(4484) 評論(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呀-__-

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


          網站導航:
           
          主站蜘蛛池模板: 开平市| 萨嘎县| 祁东县| 习水县| 金门县| 寿阳县| 义乌市| 仲巴县| 吴堡县| 乌拉特后旗| 武汉市| 浮山县| 白城市| 抚顺市| 绍兴县| 莱阳市| 通许县| 新蔡县| 色达县| 彭山县| 黔江区| 丰城市| 浦江县| 朝阳县| 乐至县| 凤山县| 东乡县| 中宁县| 马山县| 镇赉县| 深圳市| 陈巴尔虎旗| 连平县| 石楼县| 南溪县| 平武县| 张北县| 龙南县| 临武县| 策勒县| 博客|