网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

使用Java怎么实现一个麦克风录音功能-创新互联

本篇文章为大家展示了使用Java怎么实现一个麦克风录音功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元抚顺县做网站,已为上家服务,为抚顺县各地企业和个人服务,联系电话:028-86922220
public class EngineeCore {

  String filePath = "E:\\voice\\voice_cache.wav";

  AudioFormat audioFormat;
  TargetDataLine targetDataLine;
  boolean flag = true;
private void stopRecognize() {
    flag = false;
    targetDataLine.stop();
    targetDataLine.close();
  }private AudioFormat getAudioFormat() {
    float sampleRate = 16000;
    // 8000,11025,16000,22050,44100
    int sampleSizeInBits = 16;
    // 8,16
    int channels = 1;
    // 1,2
    boolean signed = true;
    // true,false
    boolean bigEndian = false;
    // true,false
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
  }// end getAudioFormat


  private void startRecognize() {
    try {
      // 获得指定的音频格式
      audioFormat = getAudioFormat();
      DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
      targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);

      // Create a thread to capture the microphone
      // data into an audio file and start the
      // thread running. It will run until the
      // Stop button is clicked. This method
      // will return after starting the thread.
      flag = true;
      new CaptureThread().start();
    } catch (Exception e) {
      e.printStackTrace();
    } // end catch
  }// end captureAudio method

  class CaptureThread extends Thread {
    public void run() {
      AudioFileFormat.Type fileType = null;
      File audioFile = new File(filePath);

      fileType = AudioFileFormat.Type.WAVE;
      //声音录入的权值
      int weight = 2;
      //判断是否停止的计数
      int downSum = 0;

      ByteArrayInputStream bais = null;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      AudioInputStream ais = null;
      try {
        targetDataLine.open(audioFormat);
        targetDataLine.start();
        byte[] fragment = new byte[1024];

        ais = new AudioInputStream(targetDataLine);
        while (flag) {

          targetDataLine.read(fragment, 0, fragment.length);
          //当数组末位大于weight时开始存储字节(有声音传入),一旦开始不再需要判断末位
          if (Math.abs(fragment[fragment.length-1]) > weight || baos.size() > 0) {
            baos.write(fragment);
            System.out.println("守卫:"+fragment[0]+",末尾:"+fragment[fragment.length-1]+",lenght"+fragment.length);
            //判断语音是否停止
            if(Math.abs(fragment[fragment.length-1])<=weight){
              downSum++;
            }else{
              System.out.println("重置奇数");
              downSum=0;
            }               //计数超过20说明此段时间没有声音传入(值也可更改)
            if(downSum>20){
              System.out.println("停止录入");
              break;
            }

          }
        }

        //取得录音输入流
        audioFormat = getAudioFormat();
        byte audioData[] = baos.toByteArray();
        bais = new ByteArrayInputStream(audioData);
        ais = new AudioInputStream(bais, audioFormat, audioData.length / audioFormat.getFrameSize());
        //定义最终保存的文件名
        System.out.println("开始生成语音文件");
        AudioSystem.write(ais, AudioFileFormat.Type.WAVE, audioFile);
        downSum = 0;
        stopRecognize();

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        //关闭流

        try {
          ais.close();
          bais.close();
          baos.reset();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

    }// end run
  }// end inner class CaptureThread

接下来测试

  public static void main(String args[]) {
    EngineeCore engineeCore = new EngineeCore();

      engineeCore.startRecognize();

  }

当有较高的声音传入麦克风时,targetDataLine读取的字节数组首位或末位绝对值会变大(位置取决于音频格式中的一些参数,如bigEndian)。传入音量低,绝对值会变小

录音开始。从targetDataLine中读取的音频数据被保存在ByteArrayOutputStream中。一段时间音量一直低于权值时,认为无声音传入,结束录音。从ByteArrayOutputStream取出字节数组,

转为音频保存在本地文件中。

注意:

从targetDataLine读取的字节数组不能直接用于百度等语音识别,需要先转为音频文件,然后读取音频文件生成的字节数组,才可用于语音识别。

上述内容就是使用Java怎么实现一个麦克风录音功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


分享名称:使用Java怎么实现一个麦克风录音功能-创新互联
分享链接:http://bjjierui.cn/article/cdshcc.html

其他资讯