欢迎光临
我们一直在努力

如何捕获音频及输出音频。-JSP教程,Application/Applet

建站超值云服务器,限时71元/月

这是我原来用过的两段代码,输出音频和捕获音频。
构造器里的socket是用来接受来自网络的音频数据。不做网络音频可以去掉它。

希望能与大家分享经验。8-)

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;

       playback(socket s){//构造器 取得socket以获得网络输入流
         this.s=s;
       }
       public void start() {

           thread = new thread(this);
           thread.setname("playback");
           thread.start();
       }

       public void stop() {
           thread = null;
       }

       public void run() {

           audioformat format =new audioformat(8000,16,2,true,true);//audioformat(float samplerate, int samplesizeinbits, int channels, boolean signed, boolean bigendian)
           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;
       }
}

import java.io.*;
import javax.sound.sampled.*;
import java.net.*;

/**
* title:        voicechat
* description:  音频捕捉(录音程序)
* copyright:    copyright (c) 2001
* company:
* @author       你猜!
* @version 1.0
*/

class capture implements runnable {

       targetdataline line;
       thread thread;
       socket s;
       bufferedoutputstream captrueoutputstream;

       capture(socket s){//构造器 取得socket以获得网络输出流
         this.s=s;
       }

       public void start() {

           thread = new thread(this);
           thread.setname("capture");
           thread.start();
       }

       public void stop() {
           thread = null;
       }

       public void run() {

           try {
             captrueoutputstream=new bufferedoutputstream(s.getoutputstream());//建立输出流 此处可以加套压缩流用来压缩数据
           }
           catch (ioexception ex) {
               return;
           }

           audioformat format =new audioformat(8000,16,2,true,true);//audioformat(float samplerate, int samplesizeinbits, int channels, boolean signed, boolean bigendian)
           dataline.info info = new dataline.info(targetdataline.class,format);

           try {
               line = (targetdataline) audiosystem.getline(info);
               line.open(format, line.getbuffersize());
           } catch (exception ex) {
               return;
           }

           byte[] data = new byte[1024];//此处的1024可以情况进行调整,应跟下面的1024应保持一致
           int numbytesread=0;
           line.start();

           while (thread != null) {
               numbytesread = line.read(data, 0,1024);//取数据(1024)的大小直接关系到传输的速度,一般越小越快,
               try {
                 captrueoutputstream.write(data, 0, numbytesread);//写入网络流
               }
               catch (exception ex) {
                   break;
               }
           }

           line.stop();
           line.close();
           line = null;

           try {
               captrueoutputstream.flush();
               captrueoutputstream.close();
           } catch (ioexception ex) {
               ex.printstacktrace();
           }
       }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何捕获音频及输出音频。-JSP教程,Application/Applet
分享到: 更多 (0)