AbstractLine不支持的控件类型:Master Gain Java

时间:2012-12-28 17:36:31

标签: java clip

我按照Clip.getControl()使用时遇到错误: -

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class HitPlayer {
    private AudioInputStream ais;
    private Clip clip;
    private FloatControl gain;

    public HitPlayer(String fname){
        try {
            ais = AudioSystem.getAudioInputStream(new File(fname));
            clip = AudioSystem.getClip();
            gain = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        } catch(UnsupportedAudioFileException e){
            System.err.println("File format not accepted");
        } catch(IOException e){
            System.err.println("IO error");
        } catch(LineUnavailableException e){
            System.err.println("Line unavailable");
        }
    }

    public void setGain(float db){
        gain.setValue(db);
    }

    public void reset(){
        try {
            ais.reset();
        } catch (IOException e) {
            System.err.println("IO error when resetting");
        }
    }

    public void play(){
        clip.start();
    }
}

堆栈跟踪如下: -

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported control type: Master Gain
at com.sun.media.sound.AbstractLine.getControl(AbstractLine.java:164)
at beat.HitPlayer.<init>(HitPlayer.java:21)
at beat.DrumMachine.<init>(DrumMachine.java:18)
at beat.Main.main(Main.java:14)

我一直在尝试通过在grepcode上检查OpenJDK的代码来跟踪错误; AbstractLine grepcode.com和相关课程。

我想知道是否有人遇到过这个问题,我正在使用 java version "1.7.0_07" OpenJDK Runtime Environment (IcedTea7 2.3.2) (Slackware) OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)如果这有所不同。

我基本上试图通过gain = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);访问增益或音量控制。无论我使用的是MASTER_GAIN还是VOLUME

,错误仍然存​​在

此致

1 个答案:

答案 0 :(得分:2)

我忘了打开这条线。添加了以下内容: -

clip.open(ais);