Java implementation of dynamic recording, sound card with sound in the automatic recording

Source: Internet
Author: User

The

Targetdataline is the input (microphone) of the sound, while the sourcedataline is the output (stereo, headset).
for the entire procedure, see the following procedure. (after running, real-time recording from the microphone, real-time output).

Import java.io.*;
Import Javax.sound.sampled.AudioSystem;
Import Javax.sound.sampled.AudioInputStream;
Import Javax.sound.sampled.AudioFormat;
Import Javax.sound.sampled.SourceDataLine;
Import Javax.sound.sampled.DataLine;
Import Javax.sound.sampled.FloatControl;
Import Javax.sound.sampled.TargetDataLine;
    public class Recordandplay {static volatile Boolean stop=false;
    public static void Main (string[] args) {play ();                    //Play audio files public static void plays () {try {Audioformat Audioformat =//
             New Audioformat (AudioFormat.Encoding.PCM_SIGNED, 44100F,//8, 1, 1, 44100F, false);
            New Audioformat (AUDIOFORMAT.ENCODING.PCM_SIGNED,44100F, 2, 4, 44100F, true); Dataline.info Info = new Dataline.info (Targetdataline.class, Audioformat);
            Targetdataline targetdataline = (targetdataline) audiosystem.getline (info);
            Targetdataline.open (Audioformat); Final Sourcedataline Sourcedataline;
            info = new Dataline.info (Sourcedataline.class, Audioformat);
            Sourcedataline = (sourcedataline) audiosystem.getline (info);
            Sourcedataline.open (Audioformat);
            Targetdataline.start ();
            Sourcedataline.start ();
            Floatcontrol fc= (Floatcontrol) Sourcedataline.getcontrol (FloatControl.Type.MASTER_GAIN);
            Double value=2;
            float DB = (float) (Math.log (Value==0.0?0.0001:value)/math.log (10.0) *20.0);
            Fc.setvalue (DB);
            int nbyte = 0;
            final int bufsize=4*100;
            byte[] buffer = new Byte[bufsize];
                while (Nbyte!=-1) {//system.in.read ();
                Nbyte = targetdataline.read (buffer, 0, bufsize);
            Sourcedataline.write (buffer, 0, nbyte);

        } sourcedataline.stop ();
        catch (Exception e) {e.printstacktrace ();
 }
    }
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.