2009-06-04 71 views

回答

34

後,可以設置與MASTER_GAIN控制音量。

import javax.sound.sampled.*; 

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
    new File("some_file.wav")); 
Clip clip = AudioSystem.getClip(); 
clip.open(audioInputStream); 
FloatControl gainControl = 
    (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); 
gainControl.setValue(-10.0f); // Reduce volume by 10 decibels. 
clip.start(); 
+0

Thanx!我會試試這個。對於我在兩端看問題的價值。我有一個iPhone客戶端攝取音頻流,只是通過在客戶端上使用不同的API,我獲得了不同的音量。所以我不認爲我的問題完全是服務器端。 – Cliff 2009-06-05 13:48:33

+4

@ markusk如果問題是特定於音量的,應該使用「FloatControl.Type.VOLUME」? – 2011-08-02 17:39:47

9

可以調整使用增益控制量,嘗試這樣的事情你如果你使用的是Java API的聲音開行

FloatControl volume= (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); 
1
public final class VolumeControl 
{ 

    private VolumeControl(){} 

    private static LinkedList<Line> speakers = new LinkedList<Line>(); 

    private final static void findSpeakers() 
    { 
     Mixer.Info[] mixers = AudioSystem.getMixerInfo(); 

     for (Mixer.Info mixerInfo : mixers) 
     { 
      if(!mixerInfo.getName().equals("Java Sound Audio Engine")) continue; 

      Mixer mixer   = AudioSystem.getMixer(mixerInfo); 
      Line.Info[] lines = mixer.getSourceLineInfo(); 

      for (Line.Info info : lines) 
      { 

       try 
       { 
        Line line = mixer.getLine(info); 
        speakers.add(line); 

       } 
       catch (LineUnavailableException e)  { e.printStackTrace();                     } 
       catch (IllegalArgumentException iaEx) {                          } 
      } 
     } 
    } 

    static 
    { 
     findSpeakers(); 
    } 

    public static void setVolume(float level) 
    { 
     System.out.println("setting volume to "+level); 
     for(Line line : speakers) 
     { 
      try 
      { 
       line.open(); 
       FloatControl control = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN); 
       control.setValue(limit(control,level)); 
      } 
      catch (LineUnavailableException e) { continue; } 
      catch(java.lang.IllegalArgumentException e) { continue; } 



     } 
    } 

    private static float limit(FloatControl control,float level) 
    { return Math.min(control.getMaximum(), Math.max(control.getMinimum(), level)); } 

}