2020-03-15 11:27:51 +01:00
|
|
|
import 'dart:async';
|
2023-02-15 15:46:14 +01:00
|
|
|
import 'dart:io';
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2023-02-15 15:46:14 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-03-15 11:27:51 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2022-03-13 07:54:44 +01:00
|
|
|
import 'package:just_audio/just_audio.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2023-02-15 15:46:14 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2022-12-30 10:36:54 +01:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2023-05-16 18:45:39 +02:00
|
|
|
import 'package:fluffychat/utils/error_reporter.dart';
|
2022-01-28 18:41:24 +01:00
|
|
|
import 'package:fluffychat/utils/localized_exception_extension.dart';
|
2022-12-30 17:54:01 +01:00
|
|
|
import '../../../utils/matrix_sdk_extensions/event_extension.dart';
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2021-04-30 17:22:29 +02:00
|
|
|
class AudioPlayerWidget extends StatefulWidget {
|
2020-03-15 11:27:51 +01:00
|
|
|
final Color color;
|
2020-03-29 20:13:25 +02:00
|
|
|
final Event event;
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2022-01-01 14:17:31 +01:00
|
|
|
static String? currentId;
|
2020-03-15 14:15:45 +01:00
|
|
|
|
2022-01-01 22:08:19 +01:00
|
|
|
static const int wavesCount = 40;
|
|
|
|
|
2022-01-01 14:17:31 +01:00
|
|
|
const AudioPlayerWidget(this.event, {this.color = Colors.black, Key? key})
|
2020-03-15 11:27:51 +01:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
2022-08-14 16:59:21 +02:00
|
|
|
AudioPlayerState createState() => AudioPlayerState();
|
2020-03-15 11:27:51 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:37:15 +02:00
|
|
|
enum AudioPlayerStatus { notDownloaded, downloading, downloaded }
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2022-08-14 16:59:21 +02:00
|
|
|
class AudioPlayerState extends State<AudioPlayerWidget> {
|
2021-04-14 10:37:15 +02:00
|
|
|
AudioPlayerStatus status = AudioPlayerStatus.notDownloaded;
|
2023-02-14 12:54:57 +01:00
|
|
|
AudioPlayer? audioPlayer;
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2022-01-01 14:17:31 +01:00
|
|
|
StreamSubscription? onAudioPositionChanged;
|
|
|
|
StreamSubscription? onDurationChanged;
|
|
|
|
StreamSubscription? onPlayerStateChanged;
|
|
|
|
StreamSubscription? onPlayerError;
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2022-01-01 14:17:31 +01:00
|
|
|
String? statusText;
|
|
|
|
int currentPosition = 0;
|
2020-03-15 11:27:51 +01:00
|
|
|
double maxPosition = 0;
|
|
|
|
|
2023-02-15 15:46:14 +01:00
|
|
|
MatrixFile? matrixFile;
|
|
|
|
File? audioFile;
|
2021-04-30 17:22:29 +02:00
|
|
|
|
2020-03-15 11:27:51 +01:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2023-02-14 12:54:57 +01:00
|
|
|
if (audioPlayer?.playerState.playing == true) {
|
|
|
|
audioPlayer?.stop();
|
2021-01-23 11:17:34 +01:00
|
|
|
}
|
2021-04-30 17:22:29 +02:00
|
|
|
onAudioPositionChanged?.cancel();
|
|
|
|
onDurationChanged?.cancel();
|
|
|
|
onPlayerStateChanged?.cancel();
|
|
|
|
onPlayerError?.cancel();
|
|
|
|
|
2020-03-15 11:27:51 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
Future<void> _downloadAction() async {
|
2021-04-14 10:37:15 +02:00
|
|
|
if (status != AudioPlayerStatus.notDownloaded) return;
|
|
|
|
setState(() => status = AudioPlayerStatus.downloading);
|
2020-12-25 09:58:34 +01:00
|
|
|
try {
|
2022-11-21 18:16:46 +01:00
|
|
|
final matrixFile = await widget.event.downloadAndDecryptAttachment();
|
2023-02-15 15:46:14 +01:00
|
|
|
File? file;
|
|
|
|
|
|
|
|
if (!kIsWeb) {
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
final fileName = Uri.encodeComponent(
|
2023-03-02 10:57:52 +01:00
|
|
|
widget.event.attachmentOrThumbnailMxcUrl()!.pathSegments.last,
|
|
|
|
);
|
2023-02-15 15:46:14 +01:00
|
|
|
file = File('${tempDir.path}/${fileName}_${matrixFile.name}');
|
|
|
|
await file.writeAsBytes(matrixFile.bytes);
|
|
|
|
}
|
|
|
|
|
2020-12-25 09:58:34 +01:00
|
|
|
setState(() {
|
2023-02-15 15:46:14 +01:00
|
|
|
audioFile = file;
|
|
|
|
this.matrixFile = matrixFile;
|
2021-04-14 10:37:15 +02:00
|
|
|
status = AudioPlayerStatus.downloaded;
|
2020-12-25 09:58:34 +01:00
|
|
|
});
|
|
|
|
_playAction();
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().v('Could not download audio file', e, s);
|
2021-05-23 13:11:55 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2021-04-03 13:09:20 +02:00
|
|
|
SnackBar(
|
2022-01-28 18:41:24 +01:00
|
|
|
content: Text(e.toLocalizedString(context)),
|
2021-04-03 13:09:20 +02:00
|
|
|
),
|
|
|
|
);
|
2020-12-25 09:58:34 +01:00
|
|
|
}
|
2020-03-15 11:27:51 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
void _playAction() async {
|
2023-02-14 12:54:57 +01:00
|
|
|
final audioPlayer = this.audioPlayer ??= AudioPlayer();
|
2021-04-30 17:22:29 +02:00
|
|
|
if (AudioPlayerWidget.currentId != widget.event.eventId) {
|
|
|
|
if (AudioPlayerWidget.currentId != null) {
|
2022-03-13 07:54:44 +01:00
|
|
|
if (audioPlayer.playerState.playing) {
|
2021-04-30 17:22:29 +02:00
|
|
|
await audioPlayer.stop();
|
2022-01-01 14:17:31 +01:00
|
|
|
setState(() {});
|
2020-03-15 12:00:25 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-30 17:22:29 +02:00
|
|
|
AudioPlayerWidget.currentId = widget.event.eventId;
|
2020-03-15 12:00:25 +01:00
|
|
|
}
|
2022-03-13 07:54:44 +01:00
|
|
|
if (audioPlayer.playerState.playing) {
|
|
|
|
await audioPlayer.pause();
|
|
|
|
return;
|
|
|
|
} else if (audioPlayer.position != Duration.zero) {
|
|
|
|
await audioPlayer.play();
|
|
|
|
return;
|
2020-03-15 11:27:51 +01:00
|
|
|
}
|
2022-03-13 07:54:44 +01:00
|
|
|
|
|
|
|
onAudioPositionChanged ??= audioPlayer.positionStream.listen((state) {
|
2022-12-30 10:51:26 +01:00
|
|
|
if (maxPosition <= 0) return;
|
2022-03-13 07:54:44 +01:00
|
|
|
setState(() {
|
|
|
|
statusText =
|
|
|
|
'${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}';
|
|
|
|
currentPosition = ((state.inMilliseconds.toDouble() / maxPosition) *
|
|
|
|
AudioPlayerWidget.wavesCount)
|
|
|
|
.round();
|
|
|
|
});
|
|
|
|
});
|
2022-12-30 10:51:26 +01:00
|
|
|
onDurationChanged ??= audioPlayer.durationStream.listen((max) {
|
|
|
|
if (max == null || max == Duration.zero) return;
|
|
|
|
setState(() => maxPosition = max.inMilliseconds.toDouble());
|
|
|
|
});
|
2022-03-13 07:54:44 +01:00
|
|
|
onPlayerStateChanged ??=
|
|
|
|
audioPlayer.playingStream.listen((_) => setState(() {}));
|
2023-02-15 15:46:14 +01:00
|
|
|
final audioFile = this.audioFile;
|
|
|
|
if (audioFile != null) {
|
|
|
|
audioPlayer.setFilePath(audioFile.path);
|
|
|
|
} else {
|
|
|
|
await audioPlayer.setAudioSource(MatrixFileAudioSource(matrixFile!));
|
|
|
|
}
|
2023-05-16 18:45:39 +02:00
|
|
|
audioPlayer.play().onError(
|
|
|
|
ErrorReporter(context, 'Unable to play audio message')
|
|
|
|
.onErrorCallback,
|
|
|
|
);
|
2020-03-15 11:27:51 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:53:31 +02:00
|
|
|
static const double buttonSize = 36;
|
|
|
|
|
2022-01-01 14:17:31 +01:00
|
|
|
String? get _durationString {
|
2021-10-27 16:53:31 +02:00
|
|
|
final durationInt = widget.event.content
|
|
|
|
.tryGetMap<String, dynamic>('info')
|
|
|
|
?.tryGet<int>('duration');
|
|
|
|
if (durationInt == null) return null;
|
|
|
|
final duration = Duration(milliseconds: durationInt);
|
|
|
|
return '${duration.inMinutes.toString().padLeft(2, '0')}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:23:17 +01:00
|
|
|
List<int> _getWaveform() {
|
2022-01-01 14:17:31 +01:00
|
|
|
final eventWaveForm = widget.event.content
|
|
|
|
.tryGetMap<String, dynamic>('org.matrix.msc1767.audio')
|
|
|
|
?.tryGetList<int>('waveform');
|
|
|
|
if (eventWaveForm == null) {
|
2022-01-01 22:08:19 +01:00
|
|
|
return List<int>.filled(AudioPlayerWidget.wavesCount, 500);
|
2022-01-01 14:17:31 +01:00
|
|
|
}
|
2022-01-01 22:08:19 +01:00
|
|
|
while (eventWaveForm.length < AudioPlayerWidget.wavesCount) {
|
2022-01-01 14:17:31 +01:00
|
|
|
for (var i = 0; i < eventWaveForm.length; i = i + 2) {
|
|
|
|
eventWaveForm.insert(i, eventWaveForm[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var i = 0;
|
2022-01-01 22:08:19 +01:00
|
|
|
final step = (eventWaveForm.length / AudioPlayerWidget.wavesCount).round();
|
|
|
|
while (eventWaveForm.length > AudioPlayerWidget.wavesCount) {
|
2022-01-01 14:17:31 +01:00
|
|
|
eventWaveForm.removeAt(i);
|
2022-01-01 22:08:19 +01:00
|
|
|
i = (i + step) % AudioPlayerWidget.wavesCount;
|
2022-01-01 14:17:31 +01:00
|
|
|
}
|
2022-01-02 10:11:55 +01:00
|
|
|
return eventWaveForm.map((i) => i > 1024 ? 1024 : i).toList();
|
2022-01-01 14:17:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 16:23:17 +01:00
|
|
|
late final List<int> waveform;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
waveform = _getWaveform();
|
|
|
|
}
|
|
|
|
|
2020-03-15 11:27:51 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-01-01 14:17:31 +01:00
|
|
|
final statusText = this.statusText ??= _durationString ?? '00:00';
|
2021-10-27 16:53:31 +02:00
|
|
|
return Padding(
|
2022-12-30 10:36:54 +01:00
|
|
|
padding: EdgeInsets.all(16 * AppConfig.bubbleSizeFactor),
|
2021-10-27 16:53:31 +02:00
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
2022-12-30 10:36:54 +01:00
|
|
|
const SizedBox(width: 4),
|
2021-10-27 16:53:31 +02:00
|
|
|
SizedBox(
|
|
|
|
width: buttonSize,
|
|
|
|
height: buttonSize,
|
|
|
|
child: status == AudioPlayerStatus.downloading
|
|
|
|
? CircularProgressIndicator(strokeWidth: 2, color: widget.color)
|
|
|
|
: InkWell(
|
|
|
|
borderRadius: BorderRadius.circular(64),
|
|
|
|
child: Material(
|
|
|
|
color: widget.color.withAlpha(64),
|
|
|
|
borderRadius: BorderRadius.circular(64),
|
|
|
|
child: Icon(
|
2023-02-14 12:54:57 +01:00
|
|
|
audioPlayer?.playerState.playing == true
|
2021-10-27 16:53:31 +02:00
|
|
|
? Icons.pause_outlined
|
|
|
|
: Icons.play_arrow_outlined,
|
|
|
|
color: widget.color,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onLongPress: () => widget.event.saveFile(context),
|
|
|
|
onTap: () {
|
|
|
|
if (status == AudioPlayerStatus.downloaded) {
|
|
|
|
_playAction();
|
|
|
|
} else {
|
|
|
|
_downloadAction();
|
|
|
|
}
|
|
|
|
},
|
2020-03-15 11:27:51 +01:00
|
|
|
),
|
2020-03-15 11:47:35 +01:00
|
|
|
),
|
2022-01-01 14:17:31 +01:00
|
|
|
const SizedBox(width: 8),
|
2021-10-27 16:53:31 +02:00
|
|
|
Expanded(
|
2022-01-01 14:17:31 +01:00
|
|
|
child: Row(
|
|
|
|
children: [
|
2022-01-01 22:08:19 +01:00
|
|
|
for (var i = 0; i < AudioPlayerWidget.wavesCount; i++)
|
2022-01-01 14:17:31 +01:00
|
|
|
Expanded(
|
|
|
|
child: InkWell(
|
2023-03-02 10:57:52 +01:00
|
|
|
onTap: () => audioPlayer?.seek(
|
|
|
|
Duration(
|
2022-01-01 22:08:19 +01:00
|
|
|
milliseconds:
|
|
|
|
(maxPosition / AudioPlayerWidget.wavesCount)
|
|
|
|
.round() *
|
2023-03-02 10:57:52 +01:00
|
|
|
i,
|
|
|
|
),
|
|
|
|
),
|
2022-03-28 20:13:20 +02:00
|
|
|
child: Container(
|
|
|
|
height: 32,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Opacity(
|
|
|
|
opacity: currentPosition > i ? 1 : 0.5,
|
|
|
|
child: Container(
|
2023-03-02 10:57:52 +01:00
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 1),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: widget.color,
|
|
|
|
borderRadius: BorderRadius.circular(64),
|
|
|
|
),
|
|
|
|
height: 32 * (waveform[i] / 1024),
|
|
|
|
),
|
2022-03-28 20:13:20 +02:00
|
|
|
),
|
2022-01-01 14:17:31 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2021-10-27 16:53:31 +02:00
|
|
|
),
|
2020-03-15 11:27:51 +01:00
|
|
|
),
|
2022-01-01 14:17:31 +01:00
|
|
|
const SizedBox(width: 8),
|
|
|
|
Container(
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
width: 42,
|
|
|
|
child: Text(
|
|
|
|
statusText,
|
|
|
|
style: TextStyle(
|
|
|
|
color: widget.color,
|
|
|
|
),
|
2021-10-27 16:53:31 +02:00
|
|
|
),
|
2021-07-18 11:34:47 +02:00
|
|
|
),
|
2021-10-27 16:53:31 +02:00
|
|
|
],
|
|
|
|
),
|
2020-03-15 11:27:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 09:15:47 +01:00
|
|
|
|
|
|
|
/// To use a MatrixFile as an AudioSource for the just_audio package
|
|
|
|
class MatrixFileAudioSource extends StreamAudioSource {
|
|
|
|
final MatrixFile file;
|
|
|
|
MatrixFileAudioSource(this.file);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<StreamAudioResponse> request([int? start, int? end]) async {
|
|
|
|
start ??= 0;
|
|
|
|
end ??= file.bytes.length;
|
|
|
|
return StreamAudioResponse(
|
|
|
|
sourceLength: file.bytes.length,
|
|
|
|
contentLength: end - start,
|
|
|
|
offset: start,
|
|
|
|
stream: Stream.value(file.bytes.sublist(start, end)),
|
|
|
|
contentType: file.mimeType,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|