使用Java怎么實(shí)現(xiàn)一個麥克風(fēng)錄音功能-創(chuàng)新互聯(lián)

本篇文章為大家展示了使用Java怎么實(shí)現(xiàn)一個麥克風(fēng)錄音功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元撫順縣做網(wǎng)站,已為上家服務(wù),為撫順縣各地企業(yè)和個人服務(wù),聯(lián)系電話: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;
      //聲音錄入的權(quán)值
      int weight = 2;
      //判斷是否停止的計(jì)數(shù)
      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);
          //當(dāng)數(shù)組末位大于weight時開始存儲字節(jié)(有聲音傳入),一旦開始不再需要判斷末位
          if (Math.abs(fragment[fragment.length-1]) > weight || baos.size() > 0) {
            baos.write(fragment);
            System.out.println("守衛(wèi):"+fragment[0]+",末尾:"+fragment[fragment.length-1]+",lenght"+fragment.length);
            //判斷語音是否停止
            if(Math.abs(fragment[fragment.length-1])<=weight){
              downSum++;
            }else{
              System.out.println("重置奇數(shù)");
              downSum=0;
            }               //計(jì)數(shù)超過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 {
        //關(guān)閉流

        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();

  }

當(dāng)有較高的聲音傳入麥克風(fēng)時,targetDataLine讀取的字節(jié)數(shù)組首位或末位絕對值會變大(位置取決于音頻格式中的一些參數(shù),如bigEndian)。傳入音量低,絕對值會變小

錄音開始。從targetDataLine中讀取的音頻數(shù)據(jù)被保存在ByteArrayOutputStream中。一段時間音量一直低于權(quán)值時,認(rèn)為無聲音傳入,結(jié)束錄音。從ByteArrayOutputStream取出字節(jié)數(shù)組,

轉(zhuǎn)為音頻保存在本地文件中。

注意:

從targetDataLine讀取的字節(jié)數(shù)組不能直接用于百度等語音識別,需要先轉(zhuǎn)為音頻文件,然后讀取音頻文件生成的字節(jié)數(shù)組,才可用于語音識別。

上述內(nèi)容就是使用Java怎么實(shí)現(xiàn)一個麥克風(fēng)錄音功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享名稱:使用Java怎么實(shí)現(xiàn)一個麥克風(fēng)錄音功能-創(chuàng)新互聯(lián)
本文URL:http://muchs.cn/article22/cdshcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、虛擬主機(jī)移動網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)靜態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化