How to capture audio and output audio

Source: Internet
Author: User
Tags format constructor socket thread


This is the two pieces of code I used, output audio and capture audio.
The socket in the constructor is used to accept audio data from the network. Do not do network audio can remove it.

I hope to share our experience with you. 8-)

Import java.io.*;
Import javax.sound.sampled.*;
Import java.net.*;


/**
* Title:voicechat
* Description: Output audio (playback program)
* Copyright:copyright (c) 2001
* Company:
* @author you guess!
* @version 1.0
*/


Class Playback implements Runnable {

Final int bufsize = 16384;
Sourcedataline Line;
Thread thread;
Socket s;

Playback (socket s) {//constructor obtains Socket for network input stream
This.s=s;
}
public void Start () {

thread = new thread (this);
Thread.setname ("Playback");
Thread.Start ();
}

public void Stop () {
thread = NULL;
}

public void Run () {

Audioformat format =new audioformat (8000,16,2,true,true);//audioformat (float samplerate, int samplesizeinbits, int Channels, Boolean signed, Boolean Bigendian)
Bufferedinputstream Playbackinputstream;

try {
Playbackinputstream=new Bufferedinputstream (New Audioinputstream (S.getinputstream (), format,2147483647)); Encapsulated into an audio output stream, if the network stream is compressed in the need to add a set of decompression flow
}
catch (IOException ex) {
Return
}

Dataline.info Info = new Dataline.info (Sourcedataline.class,format);

try {
line = (sourcedataline) audiosystem.getline (info);
Line.open (format, bufsize);
catch (Lineunavailableexception ex) {
Return
}

byte[] data = new byte[1024];//the size of the array here is not related to real time, and can be adjusted according to the situation
int numbytesread = 0;
Line.start ();

while (thread!= null) {
try{
Numbytesread = playbackinputstream.read (data);
Line.write (data, 0,numbytesread);
catch (IOException e) {
Break
}
}

if (thread!= null) {
Line.drain ();
}

Line.stop ();
Line.close ();
line = null;
}
}


Import java.io.*;
Import javax.sound.sampled.*;
Import java.net.*;

/**
* Title:voicechat
* Description: Audio Capture (recording program)
* Copyright:copyright (c) 2001
* Company:
* @author you guess!
* @version 1.0
*/

Class Capture implements Runnable {

Targetdataline Line;
Thread thread;
Socket s;
Bufferedoutputstream Captrueoutputstream;

Capture (socket s) {//constructor obtains Socket for network output stream
This.s=s;
}

public void Start () {

thread = new thread (this);
Thread.setname ("Capture");
Thread.Start ();
}

public void Stop () {
thread = NULL;
}

public void Run () {

try {
Captrueoutputstream=new Bufferedoutputstream (S.getoutputstream ())//Create output stream Here you can add a set of compressed streams to compress the data.
}
catch (IOException ex) {
Return
}

Audioformat format =new audioformat (8000,16,2,true,true);//audioformat (float samplerate, int samplesizeinbits, int Channels, Boolean signed, Boolean Bigendian)
Dataline.info Info = new Dataline.info (Targetdataline.class,format);

try {
line = (targetdataline) audiosystem.getline (info);
Line.open (format, line.getbuffersize ());
catch (Exception ex) {
Return
}

byte[] data = new byte[1024];//Here 1024 can be adjusted, should be consistent with the following 1024
int numbytesread=0;
Line.start ();

while (thread!= null) {
Numbytesread = Line.read (data, 0,1024), and/or the size of (1024) directly related to the speed of transmission, generally the smaller the faster,
try {
Captrueoutputstream.write (data, 0, numbytesread);/write to Network stream
}
catch (Exception ex) {
Break
}
}

Line.stop ();
Line.close ();
line = null;

try {
Captrueoutputstream.flush ();
Captrueoutputstream.close ();
catch (IOException ex) {
Ex.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.