get sound Target Line - Java javax.sound.sampled

Java examples for javax.sound.sampled:Sound

Description

get sound Target Line

Demo Code


//package com.java2s;
import javax.sound.sampled.*;

public class Main {
    private static Line.Info tdInfo = new Line.Info(TargetDataLine.class);

    public static TargetDataLine getTargetLine(String nm,
            AudioFormat audioFormat)
    /* Return the target line for the mixer with a name containing
       nm, with the specified audio format */
    {//w  ww  . j a  v  a 2  s. com
        Mixer mixer = getNamedTargetLine(nm);
        if (mixer == null) {
            System.out.println("No target line mixer found for \"" + nm
                    + "\"");
            return null;
        }
        System.out.println("Recording from \""
                + mixer.getMixerInfo().getName() + "\"");

        TargetDataLine tdLine = null;
        DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                audioFormat);
        try {
            tdLine = (TargetDataLine) mixer.getLine(info);
            tdLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            System.out.println("Unable to access the capture line");
        }
        return tdLine;
    }

    public static Mixer getNamedTargetLine(String nm)
    /* look at all the mixers, and return the first one that contains nm
       in its name and is a TargetDataLine
     */
    {
        for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
            Mixer mixer = AudioSystem.getMixer(mi);
            if (isNamedTargetDataLine(mixer, nm))
                return AudioSystem.getMixer(mi);
        }
        return null;
    }

    public static boolean isNamedTargetDataLine(Mixer mixer, String nm)
    // is this a TargetDataLine containing nm
    {
        return (mixer.isLineSupported(tdInfo) && containsName(mixer, nm));
    }

    public static boolean containsName(Mixer mixer, String nm)
    /* is this a mixer with nm in its name, ignoring case */
    {
        Mixer.Info mi = mixer.getMixerInfo();
        String name = mi.getName().toLowerCase();
        return name.contains(nm.toLowerCase());
    }
}

Related Tutorials