get Audio Mute Control - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

get Audio Mute Control

Demo Code

/*//from   w  ww . ja  v  a  2s. c  om
 * Copyright 2011 Witoslaw Koczewsi <wi@koczewski.de>
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
 * General Public License as published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with this program. If not,
 * see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import javax.sound.sampled.BooleanControl;
import javax.sound.sampled.CompoundControl;
import javax.sound.sampled.Control;
import javax.sound.sampled.Control.Type;

import javax.sound.sampled.Line;

import javax.sound.sampled.Mixer;
import javax.sound.sampled.Mixer.Info;

public class Main {
    public static BooleanControl getMuteControl(Line line) {
        if (!line.isOpen())
            throw new RuntimeException("Line is closed: " + toString(line));
        return (BooleanControl) findControl(BooleanControl.Type.MUTE,
                line.getControls());
    }

    public static String toString(Control control) {
        if (control == null)
            return null;
        return control.toString() + " (" + control.getType().toString()
                + ")";
    }

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

    public static String toString(Mixer mixer) {
        if (mixer == null)
            return null;
        StringBuilder sb = new StringBuilder();
        Info info = mixer.getMixerInfo();
        sb.append(info.getName());
        sb.append(" (").append(info.getDescription()).append(")");
        sb.append(mixer.isOpen() ? " [open]" : " [closed]");
        return sb.toString();
    }

    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