Created
October 19, 2019 13:02
-
-
Save ahmadrosid/3b2105500070a55ee83def954a6f2ab0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package websocket; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.WebSocketListener; | |
import okio.ByteString; | |
import javax.sound.sampled.*; | |
import java.io.ByteArrayInputStream; | |
import java.util.concurrent.TimeUnit; | |
public class AudioClient { | |
private okhttp3.WebSocket ws; | |
private OkHttpClient okHttpClient; | |
static AudioInputStream ais; | |
static AudioFormat format; | |
static int sampleRate = 44100; | |
static DataLine.Info dataLineInfo; | |
static SourceDataLine sourceDataLine; | |
public static void main(String[] args) throws LineUnavailableException { | |
AudioClient audioClient = new AudioClient(2120); | |
} | |
private AudioClient(int port) throws LineUnavailableException { | |
okHttpClient = new OkHttpClient.Builder() | |
.readTimeout(5000, TimeUnit.MILLISECONDS) | |
.build(); | |
Request request = new Request.Builder().url("ws://192.168.0.103:" + port).build(); | |
StreamWebSocketListener listener = new StreamWebSocketListener(); | |
ws = okHttpClient.newWebSocket(request, listener); | |
okHttpClient.dispatcher().executorService().shutdown(); | |
format = new AudioFormat(sampleRate, 16, 1, true, false); | |
dataLineInfo = new DataLine.Info(SourceDataLine.class, format); | |
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); | |
sourceDataLine.open(format); | |
sourceDataLine.start(); | |
FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); | |
volumeControl.setValue(1.00f); | |
} | |
private static void toSpeaker(byte[] soundBytes) { | |
try { | |
sourceDataLine.write(soundBytes, 0, soundBytes.length); | |
} catch (Exception e) { | |
System.out.println("Not working in speakers..."); | |
e.printStackTrace(); | |
} | |
} | |
private static final class StreamWebSocketListener extends WebSocketListener { | |
@Override | |
public void onOpen(okhttp3.WebSocket webSocket, Response response) { | |
System.out.println("We are connected"); | |
} | |
@Override | |
public void onMessage(okhttp3.WebSocket webSocket, ByteString message) { | |
ByteArrayInputStream baiss = new ByteArrayInputStream(message.toByteArray()); | |
ais = new AudioInputStream(baiss, format, 1024/2); | |
toSpeaker(message.toByteArray()); | |
} | |
@Override | |
public void onClosing(okhttp3.WebSocket webSocket, int code, String reason) { | |
System.out.println("onClosing: Close connection"); | |
webSocket.close(1000, null); | |
sourceDataLine.drain(); | |
sourceDataLine.close(); | |
} | |
} | |
public void send(ByteString bytes){ | |
ws.send(bytes); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package websocket; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.WebSocketListener; | |
import okio.ByteString; | |
public class WebSocket { | |
public okhttp3.WebSocket ws; | |
private OkHttpClient okHttpClient; | |
public WebSocket(int port) { | |
okHttpClient = new OkHttpClient.Builder() | |
.readTimeout(5000, TimeUnit.MILLISECONDS) | |
.build(); | |
Request request = new Request.Builder().url("ws://192.168.0.103:" + port).build(); | |
AudioWebSocketListener listener = new AudioWebSocketListener(); | |
ws = okHttpClient.newWebSocket(request, listener); | |
okHttpClient.dispatcher().executorService().shutdown(); | |
} | |
private final class AudioWebSocketListener extends WebSocketListener { | |
@Override | |
public void onOpen(okhttp3.WebSocket webSocket, Response response) { | |
webSocket.send("Hello there"); | |
} | |
@Override | |
public void onMessage(okhttp3.WebSocket webSocket, String text) { | |
System.out.println("onMessage : " + text); | |
} | |
@Override | |
public void onClosing(okhttp3.WebSocket webSocket, int code, String reason) { | |
System.out.println("onClosing: Close connection"); | |
webSocket.close(1000, null); | |
} | |
} | |
public void send(ByteString bytes){ | |
ws.send(bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment