Cancel an active recording if the chat is switched

This commit is contained in:
Kai S. K. Engelbart 2020-07-05 12:21:07 +02:00
parent a90f58afe0
commit e4e5e6376c
3 changed files with 28 additions and 7 deletions

View File

@ -57,6 +57,12 @@ public final class AudioRecorder {
*/ */
public boolean isSupported() { return AudioSystem.isLineSupported(info); } 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. * Starts the audio recording.
* *
@ -100,4 +106,14 @@ public final class AudioRecorder {
throw new EnvoyException("Cannot save voice recording", e); 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();
}
} }

View File

@ -17,7 +17,9 @@ import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color; 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.data.audio.AudioRecorder;
import envoy.client.event.MessageCreationEvent; import envoy.client.event.MessageCreationEvent;
import envoy.client.net.Client; import envoy.client.net.Client;
@ -179,7 +181,6 @@ public final class ChatScene {
if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info"); if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info");
recorder = new AudioRecorder(); recorder = new AudioRecorder();
voiceButton.setDisable(!recorder.isSupported());
} }
/** /**
@ -211,7 +212,11 @@ public final class ChatScene {
} }
// Discard the pending attachment // Discard the pending attachment
// TODO: stop running recording if (recorder.isRecording()) {
recorder.cancel();
recording = false;
voiceButton.setText("Record Voice Message");
}
pendingAttachment = null; pendingAttachment = null;
remainingChars.setVisible(true); 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)); .setText(String.format("remaining chars: %d/%d", MAX_MESSAGE_LENGTH - messageTextArea.getText().length(), MAX_MESSAGE_LENGTH));
} }
messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled);
voiceButton.setDisable(!recorder.isSupported());
} }
/** /**
@ -290,8 +296,7 @@ public final class ChatScene {
private void checkPostConditions(boolean sendKeyPressed) { private void checkPostConditions(boolean sendKeyPressed) {
if (!postingPermanentlyDisabled) { if (!postingPermanentlyDisabled) {
if (!postButton.isDisabled() && sendKeyPressed) if (!postButton.isDisabled() && sendKeyPressed) postMessage();
postMessage();
postButton.setDisable((messageTextArea.getText().isBlank() && pendingAttachment == null) || currentChat == null); postButton.setDisable((messageTextArea.getText().isBlank() && pendingAttachment == null) || currentChat == null);
} else { } else {
final var noMoreMessaging = "Go online to send messages"; final var noMoreMessaging = "Go online to send messages";

View File

@ -111,8 +111,8 @@
<Insets right="10.0" top="5.0" /> <Insets right="10.0" top="5.0" />
</GridPane.margin> </GridPane.margin>
<buttons> <buttons>
<Button fx:id="voiceButton" onAction="#voiceButtonClicked" <Button fx:id="voiceButton" disable="true"
text="_Record Voice Message" /> onAction="#voiceButtonClicked" text="_Record Voice Message" />
<Button fx:id="postButton" defaultButton="true" <Button fx:id="postButton" defaultButton="true"
disable="true" mnemonicParsing="true" onAction="#postMessage" disable="true" mnemonicParsing="true" onAction="#postMessage"
prefHeight="10.0" prefWidth="75.0" text="_Post"> prefHeight="10.0" prefWidth="75.0" text="_Post">