欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 實現(xiàn)麥克風(fēng)自動錄音

 更新時間:2020年12月22日 17:22:35   作者:吾乃閃耀的知識燈塔  
這篇文章主要介紹了Java 實現(xiàn)麥克風(fēng)自動錄音的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下

  最近在研究語音識別,使用百度的sdk。發(fā)現(xiàn)只有識別的部分,而我需要保存音頻文件,并且實現(xiàn)當(dāng)有聲音傳入時自動生成音頻文件。

  先上代碼:

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;
      //判斷是否停止的計數(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;
            }               //計數(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ù)組,才可用于語音識別。

以上就是Java 實現(xiàn)麥克風(fēng)自動錄音的詳細(xì)內(nèi)容,更多關(guān)于Java 麥克風(fēng)自動錄音的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring?使用注解存儲和讀取?Bean對象操作方法

    Spring?使用注解存儲和讀取?Bean對象操作方法

    在?Spring?中,要想更加簡單的實現(xiàn)對?Bean?對象的儲存和使用,其核心就是使用?注解?,本文主要就是演示如何使用注解實現(xiàn)對?Bean?對象的存取操作,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • 談?wù)凧ava中try-catch-finally中的return語句

    談?wù)凧ava中try-catch-finally中的return語句

    我們知道return語句用在某一個方法中,一是用于返回函數(shù)的執(zhí)行結(jié)果,二是用于返回值為void類型的函數(shù)中,僅僅是一個return語句(return ;),此時用于結(jié)束方法的執(zhí)行,也即此return后的語句將不會被執(zhí)行,當(dāng)然,這種情況下return語句后不能再有其它的語句了
    2016-01-01
  • 如何把Spring Cloud Data Flow部署在Kubernetes上

    如何把Spring Cloud Data Flow部署在Kubernetes上

    這篇文章主要介紹了把Spring Cloud Data Flow部署在Kubernetes上,再跑個任務(wù)試試,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 詳解SpringMVC如何進(jìn)行數(shù)據(jù)回顯

    詳解SpringMVC如何進(jìn)行數(shù)據(jù)回顯

    這篇文章主要介紹了詳解SpringMVC如何進(jìn)行數(shù)據(jù)回顯,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • springboot使用Mybatis-plus分頁插件的案例詳解

    springboot使用Mybatis-plus分頁插件的案例詳解

    這篇文章主要介紹了springboot使用Mybatis-plus分頁插件的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析

    SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析

    這篇文章主要介紹了SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • JavaWeb Spring依賴注入深入學(xué)習(xí)

    JavaWeb Spring依賴注入深入學(xué)習(xí)

    這篇文章主要為大家詳細(xì)介紹了JavaWeb Spring依賴注入,深入學(xué)習(xí)Spring依賴注入,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Java中Properties類的操作實例詳解

    Java中Properties類的操作實例詳解

    這篇文章主要介紹了Java中Properties類的操作實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot如何統(tǒng)一配置bean的別名

    SpringBoot如何統(tǒng)一配置bean的別名

    這篇文章主要介紹了SpringBoot如何統(tǒng)一配置bean的別名,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 詳解Spring MVC自動為對象注入枚舉類型

    詳解Spring MVC自動為對象注入枚舉類型

    本篇文章主要介紹了Spring MVC自動為對象注入枚舉類型,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04

最新評論