Search code examples
javaclip

AbstractLine Unsupported control type: Master Gain Java


I am having an error when using Clip.getControl() as per:-

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();
    }
}

The stacktrace is as follows:-

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)

I have been trying to trace the error by checking the code for OpenJDK on grepcode; AbstractLine grepcode.com and the related classes.

I was wondering if anybody had ran into this problem, I am using 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) if that makes a difference.

I am basically trying to access the gain or volume controls via gain = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);. The error persists regardless of whether I am using MASTER_GAIN or VOLUME

Regards


Solution

  • I forgot to open the line. Added the following:-

    clip.open(ais);