This is two pieces of code that I used to output and capture audio.
The socket in the constructor is used to accept audio data from the network. You can remove it without network audio.
I hope to share my experience with you. 8 -)
Import java. io .*;
Import javax. sound. sampled .*;
Import java.net .*;
/**
* Title: VoiceChat
* Description: Output Audio (audio 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) {// The constructor retrieves the socket to obtain the 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)