⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playback.java

📁 音频捕获和播出
💻 JAVA
字号:
import java.io.*;

import javax.sound.sampled.*;

import java.net.*;

/**

* Title:        VoiceChat

* Description:  输出音频(放音程序)

* Copyright:    Copyright (c) 2001

* Company:

* @author       你猜!

* @version 1.0

*/





class Playback implements Runnable
{



       final int bufSize = 16384;

       SourceDataLine line;

       Thread thread;

       Socket s;


       //构造器 取得socket以获得网络输入流
       Playback(Socket s)
       {

         this.s=s;

       }

       public void start()
       {
           thread = new Thread(this);

           thread.setName("Playback");

           thread.start();

       }



       public void stop()
       {
           thread = null;

       }



       public void run()
       {


           //AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)
           AudioFormat format =new AudioFormat(8000,16,2,true,true);

           BufferedInputStream playbackInputStream;



           try
           {
			   //封装成音频输出流,如果网络流是经过压缩的需在此加套解压流

             playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,2147483647));

           }

           catch (IOException ex)
           {

               return;

           }



           DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);



           try
           {

               line = (SourceDataLine) AudioSystem.getLine(info);

               line.open(format, bufSize);

           }
           catch (LineUnavailableException ex)
           {

               return;

           }



           byte[] data = new byte[1024];//此处数组的大小跟实时性关系不大,可根据情况进行调整

           int numBytesRead = 0;

           line.start();



           while (thread != null)
           {

              try
              {

                 numBytesRead = playbackInputStream.read(data);

                 line.write(data, 0,numBytesRead);

              }
              catch (IOException e)
              {

                   break;

               }

           }



           if (thread != null)
           {

               line.drain();

           }



           line.stop();

           line.close();

           line = null;

       }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -