Skip to content

Commit c3ebf3e

Browse files
fix!: 🐛 Implement logic to manage voice message playback during recording and manage currently playing voice messages
1 parent 9125820 commit c3ebf3e

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* **Fix**: [423](https://github.com/SimformSolutionsPvtLtd/chatview/pull/423)
44
Rendering issue in attached image preview when sending message on web.
5+
* **Feat**: [420](https://github.com/SimformSolutionsPvtLtd/chatview/pull/420) Added support for
6+
`playerMode` in `VoiceMessageConfiguration` with `single` and `multi`.
57

68
## [3.0.0]
79

doc/documentation.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,19 @@ ChatView(
786786
},
787787
iconColor: Colors.black,
788788
),
789+
voiceMessageConfig: VoiceMessageConfiguration(
790+
// Determines whether single or multi player mode is enabled for audio playback.
791+
// Note: Defaults to multi-player mode.
792+
//
793+
// PlayerMode.single:
794+
// - Only one audio can be played at a time.
795+
// - Starting recording will stop any currently playing audio.
796+
//
797+
// PlayerMode.multi:
798+
// - Multiple audios can be played simultaneously.
799+
// - Starting recording will not affect any currently playing audio.
800+
playerMode: PlayerMode.single,
801+
)
789802
),
790803
// ...
791804
)

lib/src/models/config_models/voice_message_configuration.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import 'package:audio_waveforms/audio_waveforms.dart';
2424
import 'package:flutter/material.dart';
2525

26+
import '../../values/enumeration.dart';
2627
import '../../values/typedefs.dart';
2728

2829
/// A configuration model class for voice message bubble.
@@ -43,6 +44,7 @@ class VoiceMessageConfiguration {
4344
this.waveformMargin,
4445
this.waveformPadding,
4546
this.enableSeekGesture = true,
47+
this.playerMode = PlayerMode.multi,
4648
});
4749

4850
/// Applies style to waveform.
@@ -86,4 +88,16 @@ class VoiceMessageConfiguration {
8688

8789
/// Enable/disable seeking with gestures. Enabled by default.
8890
final bool enableSeekGesture;
91+
92+
/// Determines whether single or multi player mode is enabled for audio playback.
93+
/// Defaults to PlayerMode.multi.
94+
///
95+
/// If PlayerMode.single:
96+
/// - Only one audio can be played at a time.
97+
/// - Note: Starting recording will stop any currently playing audio.
98+
///
99+
/// If PlayerMode.multi:
100+
/// - Multiple audios can be played simultaneously.
101+
/// - Note: Starting recording will not affect any currently playing audio.
102+
final PlayerMode playerMode;
89103
}

lib/src/values/enumeration.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,27 @@ enum UserActiveStatusAlignment {
125125

126126
bool get isBottomRight => this == bottomRight;
127127
}
128+
129+
/// Defines the audio player mode,
130+
/// controlling whether single or multiple audio files can be
131+
/// played simultaneously.
132+
enum PlayerMode {
133+
/// Only one audio can be played at a time.
134+
///
135+
/// Note: Starting recording will stop any currently playing audio.
136+
single,
137+
138+
/// Multiple audios can be played simultaneously.
139+
///
140+
/// Note: Starting recording will not affect any currently playing audio.
141+
multi;
142+
}
143+
144+
/// Extension methods for PlayerMode enum.
145+
extension PlayerModeExtension on PlayerMode {
146+
/// Checks if the player mode is single.
147+
bool get isSingle => this == PlayerMode.single;
148+
149+
/// Checks if the player mode is multi.
150+
bool get isMulti => this == PlayerMode.multi;
151+
}

lib/src/widgets/chatui_textfield.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import 'package:flutter/foundation.dart';
2828
import 'package:flutter/material.dart';
2929
import 'package:flutter/services.dart';
3030

31+
import '../extensions/extensions.dart';
3132
import '../models/config_models/send_message_configuration.dart';
3233
import '../utils/constants/constants.dart';
3334
import '../utils/debounce.dart';
3435
import '../utils/package_strings.dart';
36+
import '../values/enumeration.dart';
3537
import '../values/typedefs.dart';
3638
import 'action_widgets/camera_action_button.dart';
3739
import 'action_widgets/gallery_action_button.dart';
@@ -76,6 +78,8 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
7678

7779
RecorderController? controller;
7880

81+
final playerController = PlayerController();
82+
7983
ValueNotifier<bool> isRecording = ValueNotifier(false);
8084

8185
bool Function(KeyEvent)? _keyboardHandler;
@@ -137,6 +141,7 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
137141
HardwareKeyboard.instance.removeHandler(handler);
138142
}
139143
widget.textEditingController.removeListener(_listenTextEditingController);
144+
playerController.dispose();
140145
super.dispose();
141146
}
142147

@@ -426,6 +431,11 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
426431
'Voice messages are only supported with android and ios platform',
427432
);
428433
if (!isRecording.value) {
434+
if (chatListConfig
435+
.messageConfig?.voiceMessageConfig?.playerMode.isSingle ??
436+
false) {
437+
playerController.pauseAllPlayers();
438+
}
429439
await controller?.record(
430440
recorderSettings: voiceRecordingConfig.recorderSettings,
431441
);

lib/src/widgets/voice_message_view.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'package:flutter/material.dart';
3030
import '../models/chat_bubble.dart';
3131
import '../models/config_models/message_reaction_configuration.dart';
3232
import '../models/config_models/voice_message_configuration.dart';
33+
import '../values/enumeration.dart';
3334
import 'reaction_widget.dart';
3435

3536
class VoiceMessageView extends StatefulWidget {
@@ -192,6 +193,9 @@ class _VoiceMessageViewState extends State<VoiceMessageView> {
192193
if (playerState.isInitialised ||
193194
playerState.isPaused ||
194195
playerState.isStopped) {
196+
if (widget.config?.playerMode.isSingle ?? false) {
197+
controller.pauseAllPlayers();
198+
}
195199
controller.startPlayer();
196200
controller.setFinishMode(finishMode: FinishMode.pause);
197201
} else {

0 commit comments

Comments
 (0)