From e1025a1569fd6569332077adf1faecde351a1bc9 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 5 Jul 2020 12:21:07 +0200 Subject: [PATCH] Cancel an active recording if the chat is switched --- .../envoy/client/data/audio/AudioRecorder.java | 16 ++++++++++++++++ .../envoy/client/ui/controller/ChatScene.java | 15 ++++++++++----- src/main/resources/fxml/ChatScene.fxml | 4 ++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/envoy/client/data/audio/AudioRecorder.java b/src/main/java/envoy/client/data/audio/AudioRecorder.java index b55da93..a59043f 100644 --- a/src/main/java/envoy/client/data/audio/AudioRecorder.java +++ b/src/main/java/envoy/client/data/audio/AudioRecorder.java @@ -57,6 +57,12 @@ public final class AudioRecorder { */ public boolean isSupported() { return AudioSystem.isLineSupported(info); } + /** + * @return {@code true} if the recorder is active + * @since Envoy Client v0.1-beta + */ + public boolean isRecording() { return line != null && line.isActive(); } + /** * Starts the audio recording. * @@ -100,4 +106,14 @@ public final class AudioRecorder { throw new EnvoyException("Cannot save voice recording", e); } } + + /** + * Cancels the active recording. + * + * @since Envoy Client v0.1-beta + */ + public void cancel() { + line.stop(); + line.close(); + } } diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 38a5b9a..2ebfbd8 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -17,7 +17,9 @@ import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; -import envoy.client.data.*; +import envoy.client.data.Chat; +import envoy.client.data.LocalDB; +import envoy.client.data.Settings; import envoy.client.data.audio.AudioRecorder; import envoy.client.event.MessageCreationEvent; import envoy.client.net.Client; @@ -179,7 +181,6 @@ public final class ChatScene { if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info"); recorder = new AudioRecorder(); - voiceButton.setDisable(!recorder.isSupported()); } /** @@ -211,7 +212,11 @@ public final class ChatScene { } // Discard the pending attachment - // TODO: stop running recording + if (recorder.isRecording()) { + recorder.cancel(); + recording = false; + voiceButton.setText("Record Voice Message"); + } pendingAttachment = null; remainingChars.setVisible(true); @@ -219,6 +224,7 @@ public final class ChatScene { .setText(String.format("remaining chars: %d/%d", MAX_MESSAGE_LENGTH - messageTextArea.getText().length(), MAX_MESSAGE_LENGTH)); } messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); + voiceButton.setDisable(!recorder.isSupported()); } /** @@ -290,8 +296,7 @@ public final class ChatScene { private void checkPostConditions(boolean sendKeyPressed) { if (!postingPermanentlyDisabled) { - if (!postButton.isDisabled() && sendKeyPressed) - postMessage(); + if (!postButton.isDisabled() && sendKeyPressed) postMessage(); postButton.setDisable((messageTextArea.getText().isBlank() && pendingAttachment == null) || currentChat == null); } else { final var noMoreMessaging = "Go online to send messages"; diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index 5638c7a..3239307 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -111,8 +111,8 @@ -