get Audio Volume Control - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

get Audio Volume Control

Demo Code


//package com.java2s;

import javax.sound.sampled.CompoundControl;
import javax.sound.sampled.Control;
import javax.sound.sampled.Control.Type;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;

public class Main {
    public static FloatControl getVolumeControl(Line line) {
        if (!line.isOpen())
            throw new RuntimeException("Line is closed: " + toString(line));
        return (FloatControl) findControl(FloatControl.Type.VOLUME,
                line.getControls());//from ww  w.j a  va2s  .  c  om
    }

    public static String toString(Line line) {
        if (line == null)
            return null;
        Line.Info info = line.getLineInfo();
        return info.toString();// + " (" + line.getClass().getSimpleName() + ")";
    }

    private static Control findControl(Type type, Control... controls) {
        if (controls == null || controls.length == 0)
            return null;
        for (Control control : controls) {
            if (control.getType().equals(type))
                return control;
            if (control instanceof CompoundControl) {
                CompoundControl compoundControl = (CompoundControl) control;
                Control member = findControl(type,
                        compoundControl.getMemberControls());
                if (member != null)
                    return member;
            }
        }
        return null;
    }
}

Related Tutorials