在程序中更改Java中程序的输出音频级别

时间:2015-02-27 18:17:30

标签: java audio decibel

我正在尝试更改程序的输出音频电平,最好是以分贝为单位。我需要更改整个程序的音频级别并记录级别的变化。语言是Java。有没有简单的方法来做到这一点?我用来播放声音的声音如下:

import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


public class Sound 
{
    String sounds;
    public Sound(String file)
    {
        sounds = file;
        playSound(sounds);
    }//end contructor

    public void playSound(String soundLoc)
    {  
        try
        {
            InputStream inputStream = getClass().getResourceAsStream(soundLoc);
            AudioStream audioStream = new AudioStream(inputStream);
            AudioPlayer.player.start(audioStream);
        }//end try

        catch (Exception e)
        {
        }//end catch
    }//end playSound method
}//end class Sound

1 个答案:

答案 0 :(得分:0)

您可以使用Java声音API

来使用MASTER_GAIN_CONTROL

您需要导入.... import javax.sound.sampled.*;

使用Clip对象获取剪辑,然后

public void playSound(String soundLoc)
    {  
        try
        {
            InputStream inputStream = getClass().getResourceAsStream(soundLoc);
            AudioStream audioStream = new AudioStream(inputStream);
            AudioPlayer.player.start(audioStream);
            Clip myclip = AudioSystem.getClip();
            myclip.open(audioStream);
            FloatControl audioControl = (FloatControl) myclip.getControl(FloatControl.Type.MASTER_GAIN);
             audioControl.setValue(-5.0f); //decrease volume 5 decibels
             clip.start();
        }//end try

        catch (Exception e)
        {
        }//end catch
    }//end playSound method