java socket 音频_WebSocket接收音频,并推送到声卡上

/******************************************************************************

* Compilation: javac StdAudio.java

* Execution: java StdAudio

* Dependencies: none

*

* Simple library for reading, writing, and manipulating .wav files.

*

*

* Limitations

* -----------

* - Assumes the audio is monaural, little endian, with sampling rate

* of 44,100

* - check when reading .wav files from a .jar file ?

*

******************************************************************************/

importjavax.sound.sampled.AudioFileFormat;importjavax.sound.sampled.AudioFormat;importjavax.sound.sampled.AudioInputStream;importjavax.sound.sampled.AudioSystem;importjavax.sound.sampled.Clip;importjavax.sound.sampled.DataLine;importjavax.sound.sampled.LineUnavailableException;importjavax.sound.sampled.SourceDataLine;importjavax.sound.sampled.UnsupportedAudioFileException;importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;/*** Standard audio. This class provides a basic capability for

* creating, reading, and saving audio.

*

* The audio format uses a sampling rate of 44,100 Hz, 16-bit, monaural.

*

*

* For additional documentation, see Section 1.5 of

* Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

*

*@authorRobert Sedgewick

*@authorKevin Wayne*/

public final classStdAudio {/*** The sample rate: 44,100 Hz for CD quality audio.*/

public static final int SAMPLE_RATE = 16000;//44100;

private static final int BYTES_PER_SAMPLE = 2; //16-bit audio

private static final int BITS_PER_SAMPLE = 16; //16-bit audio

private static final double MAX_16_BIT = 32768;private static final int SAMPLE_BUFFER_SIZE = 4096;private static final int MONO = 1;private static final int STEREO = 2;private static final boolean LITTLE_ENDIAN = false;private static final boolean BIG_ENDIAN = true;private static final boolean SIGNED = true;private static final boolean UNSIGNED = false;private static SourceDataLine line; //to play the sound

private static byte[] buffer; //our internal buffer

private static int bufferSize = 0; //number of samples currently in internal buffer

privateStdAudio() {//can not instantiate

}//static initializer

static{

init();

}//open up an audio stream

private static voidinit() {try{//44,100 Hz, 16-bit audio, mono, signed PCM, little endian

AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, MONO, SIGNED, LITTLE_ENDIAN);

DataLine.Info info= new DataLine.Info(SourceDataLine.class, format);

line=(SourceDataLine) AudioSystem.getLine(info);

line.open(format, SAMPLE_BUFFER_SIZE*BYTES_PER_SAMPLE);//the internal buffer is a fraction of the actual buffer size, this choice is arbitrary//it gets divided because we can't expect the buffered data to line up exactly with when//the s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Java-WebSocket库来编写多个WebSocket实例。在使用Java-WebSocket库时,需要创建多个WebSocketServer实例,并为每个实例指定不同的端口号。每个WebSocketServer实例都可以处理多个WebSocket连接。在处理WebSocket连接时,可以使用WebSocket类的onOpen、onClose、onMessage和onError方法来处理WebSocket连接的打开、关闭、消息和错误事件。此外,还可以使用WebSocket类的send方法向客户端发送消息。下面是一个简单的Java-WebSocket多个WebSocket实例的示例代码: ``` import java.net.InetSocketAddress; import java.util.concurrent.CopyOnWriteArrayList; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; public class MultiWebSocketServer { private CopyOnWriteArrayList<WebSocket> webSockets = new CopyOnWriteArrayList<>(); public MultiWebSocketServer(int... ports) { for (int port : ports) { WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) { @Override public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) { webSockets.add(webSocket); System.out.println("WebSocket opened: " + webSocket.getRemoteSocketAddress()); } @Override public void onClose(WebSocket webSocket, int i, String s, boolean b) { webSockets.remove(webSocket); System.out.println("WebSocket closed: " + webSocket.getRemoteSocketAddress()); } @Override public void onMessage(WebSocket webSocket, String s) { System.out.println("WebSocket message received: " + s); } @Override public void onError(WebSocket webSocket, Exception e) { System.out.println("WebSocket error: " + e.getMessage()); } }; server.start(); System.out.println("WebSocket server started on port " + port); } } public void broadcast(String message) { for (WebSocket webSocket : webSockets) { webSocket.send(message); } } public static void main(String[] args) { MultiWebSocketServer server = new MultiWebSocketServer(8080, 8081); server.broadcast("Hello, world!"); } } ``` 此代码创建了两个WebSocketServer实例,分别监听8080和8081端口。在每个WebSocketServer实例的onOpen、onClose、onMessage和onError方法中,分别处理WebSocket连接的打开、关闭、消息和错误事件。在broadcast方法中,使用WebSocket类的send方法向所有客户端发送消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值