| .. | .. |
|---|
| 480 | 480 | _scrollToBottom(); |
|---|
| 481 | 481 | } |
|---|
| 482 | 482 | |
|---|
| 483 | + String? _recordingSessionId; // Capture session at recording start |
|---|
| 484 | + |
|---|
| 483 | 485 | Future<void> _startRecording() async { |
|---|
| 486 | + _recordingSessionId = ref.read(activeSessionIdProvider); |
|---|
| 484 | 487 | final path = await AudioService.startRecording(); |
|---|
| 485 | 488 | if (path != null) { |
|---|
| 486 | 489 | setState(() => _isRecording = true); |
|---|
| 490 | + } else { |
|---|
| 491 | + _recordingSessionId = null; |
|---|
| 487 | 492 | } |
|---|
| 488 | 493 | } |
|---|
| 489 | 494 | |
|---|
| 490 | 495 | Future<void> _stopRecording() async { |
|---|
| 496 | + final targetSession = _recordingSessionId; |
|---|
| 497 | + _recordingSessionId = null; |
|---|
| 491 | 498 | final path = await AudioService.stopRecording(); |
|---|
| 492 | 499 | setState(() => _isRecording = false); |
|---|
| 493 | 500 | |
|---|
| .. | .. |
|---|
| 512 | 519 | 'audioBase64': b64, |
|---|
| 513 | 520 | 'content': '', |
|---|
| 514 | 521 | 'messageId': message.id, |
|---|
| 515 | | - 'sessionId': ref.read(activeSessionIdProvider), |
|---|
| 522 | + 'sessionId': targetSession, |
|---|
| 516 | 523 | }); |
|---|
| 517 | 524 | |
|---|
| 518 | 525 | _scrollToBottom(); |
|---|
| .. | .. |
|---|
| 551 | 558 | if (message.audioUri == null) return; |
|---|
| 552 | 559 | |
|---|
| 553 | 560 | // Toggle: if this message is already playing, stop it |
|---|
| 554 | | - if (_playingMessageId == message.id && AudioService.isPlaying) { |
|---|
| 561 | + if (_playingMessageId == message.id) { |
|---|
| 555 | 562 | AudioService.stopPlayback(); |
|---|
| 556 | 563 | setState(() => _playingMessageId = null); |
|---|
| 557 | 564 | return; |
|---|
| 558 | 565 | } |
|---|
| 559 | 566 | |
|---|
| 567 | + // Stop any current playback first |
|---|
| 568 | + if (_playingMessageId != null) { |
|---|
| 569 | + AudioService.stopPlayback(); |
|---|
| 570 | + } |
|---|
| 571 | + |
|---|
| 560 | 572 | setState(() => _playingMessageId = message.id); |
|---|
| 561 | 573 | |
|---|
| 562 | 574 | if (message.audioUri!.startsWith('/')) { |
|---|