-
Notifications
You must be signed in to change notification settings - Fork 29
/
Example.java
122 lines (98 loc) · 3.94 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package io.obswebsocket.community.client.example;
import static org.slf4j.LoggerFactory.getLogger;
import io.obswebsocket.community.client.OBSRemoteController;
import io.obswebsocket.community.client.message.event.ui.StudioModeStateChangedEvent;
import io.obswebsocket.community.client.message.request.ui.GetStudioModeEnabledRequest;
import io.obswebsocket.community.client.message.response.ui.GetStudioModeEnabledResponse;
import org.slf4j.Logger;
public class Example {
private static final Logger log = getLogger(Example.class);
private OBSRemoteController obsRemoteController;
private boolean isReconnecting = false;
public static void main(String[] args) {
new Example();
}
private Example() {
this.createOBSRemoteController();
// Connect
this.obsRemoteController.connect();
}
private void createOBSRemoteController() {
// Create OBSRemoteController through its builder
this.obsRemoteController = OBSRemoteController.builder()
.autoConnect(false) // Do not connect automatically
.host("127.0.0.1") // Set the host
.port(4455) // Set the port
.password("53CR37") // Set the password
.lifecycle() // Add some lifecycle callbacks
.onReady(this::onReady) // Add onReady callback
.and() // Build the LifecycleListenerBuilder
.registerEventListener(StudioModeStateChangedEvent.class,
this::onStudioModeStateChanged) // Register a StudioModeStateChangedEvent
.build(); // Build the OBSRemoteController
}
private void onReady() {
if (!this.isReconnecting) {
// First connection
this.onFirstConnection();
} else {
// Second connection
this.onSecondConnection();
}
}
private void onFirstConnection() {
// Send a blocking call (last parameter is a timeout instead of a callback)
this.obsRemoteController.setStudioModeEnabled(true, 1000);
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
// Send a request through a convenience method
this.obsRemoteController.getSceneList(getSceneListResponse -> {
if (getSceneListResponse.isSuccessful()) {
// Print each Scene
getSceneListResponse.getScenes().forEach(scene -> log.info("Scene: {}", scene));
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
this.obsRemoteController.setStudioModeEnabled(false, 1000);
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
this.disconnectAndReconnect();
});
}
private void onSecondConnection() {
// Send a Request through specific Request builder
this.obsRemoteController.sendRequest(GetStudioModeEnabledRequest.builder().build(),
(GetStudioModeEnabledResponse requestResponse) -> {
if (requestResponse.isSuccessful()) {
if (requestResponse.getStudioModeEnabled()) {
log.info("Studio mode enabled");
} else {
log.info("Studio mode not enabled");
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
// Disconnect
this.obsRemoteController.disconnect();
});
}
private void disconnectAndReconnect() {
// Disconnect
this.obsRemoteController.disconnect();
// Set a flag
this.isReconnecting = true;
// // Recreate instance
this.createOBSRemoteController();
// Reconnect
this.obsRemoteController.connect(); // onReady will be called another time
}
private void onStudioModeStateChanged(StudioModeStateChangedEvent studioModeStateChangedEvent) {
log.info(
"Studio Mode State Changed to: {}",
studioModeStateChangedEvent.getStudioModeEnabled());
}
}