神奇好望角 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呀-__-

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


          網站導航:
           
          主站蜘蛛池模板: 扬州市| 会理县| 盐亭县| 中西区| 广宁县| 潼南县| 浪卡子县| 蓬安县| 长治县| 湄潭县| 嘉禾县| 吉安县| 玛纳斯县| 柞水县| 垦利县| 永昌县| 阿拉善右旗| 杭锦后旗| 巧家县| 历史| 平顶山市| 宽甸| 平阴县| 麻栗坡县| 双鸭山市| 津市市| 萨迦县| 墨竹工卡县| 鄂州市| 新郑市| 英德市| 会宁县| 长治县| 乌海市| 五常市| 望都县| 弥勒县| 广水市| 林甸县| 长顺县| 常山县|