chore: fix audio ID issue
This commit is contained in:
parent
17adf002de
commit
964b20e074
3 changed files with 63 additions and 42 deletions
|
|
@ -488,7 +488,9 @@ class ChatController extends State<ChatPageWithRoom>
|
||||||
final botAudioEvent = timeline.events!.firstWhereOrNull(
|
final botAudioEvent = timeline.events!.firstWhereOrNull(
|
||||||
(e) =>
|
(e) =>
|
||||||
e.senderId == BotName.byEnvironment &&
|
e.senderId == BotName.byEnvironment &&
|
||||||
e.content.tryGet<String>('msgtype') == MessageTypes.Audio,
|
e.content.tryGet<String>('msgtype') == MessageTypes.Audio &&
|
||||||
|
DateTime.now().difference(e.originServerTs) <
|
||||||
|
const Duration(seconds: 10),
|
||||||
);
|
);
|
||||||
if (botAudioEvent == null) return;
|
if (botAudioEvent == null) return;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -222,10 +222,11 @@ class MessageContent extends StatelessWidget {
|
||||||
fontSize: fontSize,
|
fontSize: fontSize,
|
||||||
// #Pangea
|
// #Pangea
|
||||||
chatController: controller,
|
chatController: controller,
|
||||||
eventId: event.eventId,
|
eventId:
|
||||||
|
"${event.eventId}${overlayController != null ? '_overlay' : ''}",
|
||||||
roomId: event.room.id,
|
roomId: event.room.id,
|
||||||
senderId: event.senderId,
|
senderId: event.senderId,
|
||||||
autoplay: overlayController != null,
|
autoplay: overlayController != null && isTransitionAnimation,
|
||||||
// Pangea#
|
// Pangea#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,34 +93,13 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
|
|
||||||
Completer<String>? _transcriptionCompleter;
|
Completer<String>? _transcriptionCompleter;
|
||||||
|
|
||||||
AudioPlayer? get _audioPlayer => Matrix.of(context).audioPlayer!;
|
MatrixState? matrix;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
final matrix = Matrix.of(context);
|
matrix = Matrix.of(context);
|
||||||
matrix.audioPlayer?.dispose();
|
|
||||||
matrix.audioPlayer = AudioPlayer();
|
|
||||||
matrix.voiceMessageEventId.value =
|
|
||||||
widget.overlayController.pangeaMessageEvent?.eventId;
|
|
||||||
|
|
||||||
_onPlayerStateChanged = _audioPlayer?.playerStateStream.listen((state) {
|
|
||||||
if (state.processingState == ProcessingState.completed) {
|
|
||||||
_updateMode(null);
|
|
||||||
}
|
|
||||||
setState(() {});
|
|
||||||
});
|
|
||||||
|
|
||||||
_onAudioPositionChanged ??= _audioPlayer?.positionStream.listen((state) {
|
|
||||||
if (_audioBytes?.tokens != null) {
|
|
||||||
widget.overlayController.highlightCurrentText(
|
|
||||||
state.inMilliseconds,
|
|
||||||
_audioBytes!.tokens!,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (messageEvent?.isAudioMessage == true) {
|
if (messageEvent?.isAudioMessage == true) {
|
||||||
_fetchTranscription();
|
_fetchTranscription();
|
||||||
}
|
}
|
||||||
|
|
@ -128,9 +107,9 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_audioPlayer?.dispose();
|
matrix?.audioPlayer?.dispose();
|
||||||
Matrix.of(context).audioPlayer = null;
|
matrix?.audioPlayer = null;
|
||||||
Matrix.of(context).voiceMessageEventId.value = null;
|
matrix?.voiceMessageEventId.value = null;
|
||||||
|
|
||||||
_onPlayerStateChanged?.cancel();
|
_onPlayerStateChanged?.cancel();
|
||||||
_onAudioPositionChanged?.cancel();
|
_onAudioPositionChanged?.cancel();
|
||||||
|
|
@ -162,8 +141,8 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
|
|
||||||
if (mode == null) {
|
if (mode == null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_audioPlayer?.stop();
|
matrix?.audioPlayer?.stop();
|
||||||
_audioPlayer?.seek(null);
|
matrix?.audioPlayer?.seek(null);
|
||||||
_selectedMode = null;
|
_selectedMode = null;
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|
@ -178,8 +157,8 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
_playAudio();
|
_playAudio();
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
_audioPlayer?.stop();
|
matrix?.audioPlayer?.stop();
|
||||||
_audioPlayer?.seek(null);
|
matrix?.audioPlayer?.seek(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_selectedMode == SelectMode.practice) {
|
if (_selectedMode == SelectMode.practice) {
|
||||||
|
|
@ -243,13 +222,52 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _playAudio() async {
|
Future<void> _playAudio() async {
|
||||||
try {
|
final playerID =
|
||||||
if (_audioPlayer != null && _audioPlayer!.playerState.playing) {
|
"${widget.overlayController.pangeaMessageEvent?.eventId}_button";
|
||||||
await _audioPlayer?.pause();
|
|
||||||
|
if (matrix?.audioPlayer != null &&
|
||||||
|
matrix?.voiceMessageEventId.value == playerID) {
|
||||||
|
// If the audio player is already initialized and playing the same message, pause it
|
||||||
|
if (matrix!.audioPlayer!.playerState.playing) {
|
||||||
|
await matrix?.audioPlayer?.pause();
|
||||||
return;
|
return;
|
||||||
} else if (_audioPlayer?.position != Duration.zero) {
|
}
|
||||||
|
// If the audio player is paused, resume it
|
||||||
|
await matrix?.audioPlayer?.play();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix?.audioPlayer?.dispose();
|
||||||
|
matrix?.audioPlayer = AudioPlayer();
|
||||||
|
matrix?.voiceMessageEventId.value =
|
||||||
|
widget.overlayController.pangeaMessageEvent?.eventId;
|
||||||
|
|
||||||
|
_onPlayerStateChanged =
|
||||||
|
matrix?.audioPlayer?.playerStateStream.listen((state) {
|
||||||
|
if (state.processingState == ProcessingState.completed) {
|
||||||
|
_updateMode(null);
|
||||||
|
}
|
||||||
|
setState(() {});
|
||||||
|
});
|
||||||
|
|
||||||
|
_onAudioPositionChanged ??=
|
||||||
|
matrix?.audioPlayer?.positionStream.listen((state) {
|
||||||
|
if (_audioBytes?.tokens != null) {
|
||||||
|
widget.overlayController.highlightCurrentText(
|
||||||
|
state.inMilliseconds,
|
||||||
|
_audioBytes!.tokens!,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (matrix?.audioPlayer != null &&
|
||||||
|
matrix!.audioPlayer!.playerState.playing) {
|
||||||
|
await matrix?.audioPlayer?.pause();
|
||||||
|
return;
|
||||||
|
} else if (matrix?.audioPlayer?.position != Duration.zero) {
|
||||||
TtsController.stop();
|
TtsController.stop();
|
||||||
await _audioPlayer?.play();
|
await matrix?.audioPlayer?.play();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -260,9 +278,9 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
if (_audioBytes == null) return;
|
if (_audioBytes == null) return;
|
||||||
|
|
||||||
if (_audioFile != null) {
|
if (_audioFile != null) {
|
||||||
await _audioPlayer?.setFilePath(_audioFile!.path);
|
await matrix?.audioPlayer?.setFilePath(_audioFile!.path);
|
||||||
} else {
|
} else {
|
||||||
await _audioPlayer?.setAudioSource(
|
await matrix?.audioPlayer?.setAudioSource(
|
||||||
BytesAudioSource(
|
BytesAudioSource(
|
||||||
_audioBytes!.bytes,
|
_audioBytes!.bytes,
|
||||||
_audioBytes!.mimeType,
|
_audioBytes!.mimeType,
|
||||||
|
|
@ -271,7 +289,7 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
}
|
}
|
||||||
|
|
||||||
TtsController.stop();
|
TtsController.stop();
|
||||||
_audioPlayer?.play();
|
matrix?.audioPlayer?.play();
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
setState(() => _audioError = e.toString());
|
setState(() => _audioError = e.toString());
|
||||||
ErrorHandler.logError(
|
ErrorHandler.logError(
|
||||||
|
|
@ -441,7 +459,7 @@ class SelectModeButtonsState extends State<SelectModeButtons> {
|
||||||
|
|
||||||
if (mode == SelectMode.audio) {
|
if (mode == SelectMode.audio) {
|
||||||
return Icon(
|
return Icon(
|
||||||
_audioPlayer?.playerState.playing == true
|
matrix?.audioPlayer?.playerState.playing == true
|
||||||
? Icons.pause_outlined
|
? Icons.pause_outlined
|
||||||
: Icons.volume_up,
|
: Icons.volume_up,
|
||||||
size: 20,
|
size: 20,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue