如何用Java声音增加录音和播放音量?

时间:2019-04-11 18:26:35

标签: java audio

我使用以下代码用Java录制和播放声音,但是音量太低,如何使其变大,至少变大2.3倍?

public void Record_Audio(String File_Path,AudioFileFormat.Type File_Type)
{
  try
  {
    audioFormat=getAudioFormat();                        // Get things set up for capture
    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.
    new Record_Thread(File_Path,File_Type).start();
  } 
  catch (Exception e)
  {
    System.out.println(e);
    System.exit(0);
  }
}

private void Play_Audio_Recording()
{
  File Audio_File=new File(Current_Folder_Path+File_Name_ComboBox.getSelectedItem().toString().trim()+"."+Get_Audio_File_Type());

  try
  {
    Audio_Clip=Applet.newAudioClip(Audio_File.toURI().toURL());
    Audio_Clip.play();
  }
  catch (Exception e) { e.printStackTrace(); }
}

  class Record_Thread extends Thread
  {
    String File_Path;
    AudioFileFormat.Type File_Type;

    Record_Thread(String File_Path) { this(File_Path,AudioFileFormat.Type.WAVE); }

    Record_Thread(String File_Path,AudioFileFormat.Type File_Type)
    {
      this.File_Path=File_Path;
      this.File_Type=File_Type;
    }

    public void run()
    {
      Audio_File=new File(File_Path);

      try
      {
        targetDataLine.open(audioFormat);
        targetDataLine.start();
        AudioSystem.write(new AudioInputStream(targetDataLine),File_Type,Audio_File);
      }
      catch (Exception e) { e.printStackTrace(); }
    }
  }

1 个答案:

答案 0 :(得分:1)

您将使用FloatControl.Type(https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/FloatControl.Type.html)来设置音量或主增益。像这样:

targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
javax.sound.sampled.FloatControl c = (FloatControl)targetDataLine.getControl(FloatControl.Type.VOLUME);
c.setValue(c.getMaximum());

可能会工作。