fluffychat/lib/widgets/event_content/audio_player.dart

188 lines
5.7 KiB
Dart
Raw Normal View History

2020-03-15 10:27:51 +00:00
import 'dart:async';
2021-04-30 15:22:29 +00:00
import 'dart:io';
2020-03-15 10:27:51 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
import 'package:audioplayers/audioplayers.dart';
2021-02-13 12:27:44 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
import 'package:fluffychat/utils/sentry_controller.dart';
import '../../utils/matrix_sdk_extensions.dart/event_extension.dart';
2020-03-15 10:27:51 +00:00
2021-04-30 15:22:29 +00:00
class AudioPlayerWidget extends StatefulWidget {
2020-03-15 10:27:51 +00:00
final Color color;
2020-03-29 18:13:25 +00:00
final Event event;
2020-03-15 10:27:51 +00:00
2020-03-29 18:13:25 +00:00
static String currentId;
2020-03-15 13:15:45 +00:00
2021-04-30 15:22:29 +00:00
const AudioPlayerWidget(this.event, {this.color = Colors.black, Key key})
2020-03-15 10:27:51 +00:00
: super(key: key);
@override
_AudioPlayerState createState() => _AudioPlayerState();
}
2021-04-14 08:37:15 +00:00
enum AudioPlayerStatus { notDownloaded, downloading, downloaded }
2020-03-15 10:27:51 +00:00
2021-04-30 15:22:29 +00:00
class _AudioPlayerState extends State<AudioPlayerWidget> {
2021-04-14 08:37:15 +00:00
AudioPlayerStatus status = AudioPlayerStatus.notDownloaded;
2021-04-30 15:22:29 +00:00
final AudioPlayer audioPlayer = AudioPlayer();
2020-03-15 10:27:51 +00:00
2021-04-30 15:22:29 +00:00
StreamSubscription onAudioPositionChanged;
StreamSubscription onDurationChanged;
StreamSubscription onPlayerStateChanged;
StreamSubscription onPlayerError;
2020-03-15 10:27:51 +00:00
2020-05-13 13:58:59 +00:00
String statusText = '00:00';
2020-03-15 10:27:51 +00:00
double currentPosition = 0;
double maxPosition = 0;
2021-04-30 15:22:29 +00:00
File audioFile;
2020-03-15 10:27:51 +00:00
@override
void dispose() {
2021-06-06 08:50:26 +00:00
if (audioPlayer.state == PlayerState.PLAYING) {
2021-04-30 15:22:29 +00:00
audioPlayer.stop();
}
2021-04-30 15:22:29 +00:00
onAudioPositionChanged?.cancel();
onDurationChanged?.cancel();
onPlayerStateChanged?.cancel();
onPlayerError?.cancel();
2020-03-15 10:27:51 +00:00
super.dispose();
}
2020-05-13 13:58:59 +00:00
Future<void> _downloadAction() async {
2021-04-14 08:37:15 +00:00
if (status != AudioPlayerStatus.notDownloaded) return;
setState(() => status = AudioPlayerStatus.downloading);
2020-12-25 08:58:34 +00:00
try {
final matrixFile =
await widget.event.downloadAndDecryptAttachmentCached();
2021-04-30 15:22:29 +00:00
final tempDir = await getTemporaryDirectory();
final fileName = matrixFile.name.contains('.')
? matrixFile.name
: '${matrixFile.name}.mp3';
final file = File('${tempDir.path}/$fileName');
await file.writeAsBytes(matrixFile.bytes);
2020-12-25 08:58:34 +00:00
setState(() {
2021-04-30 15:22:29 +00:00
audioFile = file;
2021-04-14 08:37:15 +00:00
status = AudioPlayerStatus.downloaded;
2020-12-25 08:58:34 +00:00
});
_playAction();
} catch (e, s) {
Logs().v('Could not download audio file', e, s);
2021-05-23 11:11:55 +00:00
ScaffoldMessenger.of(context).showSnackBar(
2021-04-03 11:09:20 +00:00
SnackBar(
2021-08-29 13:18:02 +00:00
content: Text(e.toString()),
2021-04-03 11:09:20 +00:00
),
);
2020-12-25 08:58:34 +00:00
}
2020-03-15 10:27:51 +00:00
}
2020-05-13 13:58:59 +00:00
void _playAction() async {
2021-04-30 15:22:29 +00:00
if (AudioPlayerWidget.currentId != widget.event.eventId) {
if (AudioPlayerWidget.currentId != null) {
2021-06-06 08:50:26 +00:00
if (audioPlayer.state != PlayerState.STOPPED) {
2021-04-30 15:22:29 +00:00
await audioPlayer.stop();
2020-03-15 16:12:16 +00:00
setState(() => null);
2020-03-15 11:00:25 +00:00
}
}
2021-04-30 15:22:29 +00:00
AudioPlayerWidget.currentId = widget.event.eventId;
2020-03-15 11:00:25 +00:00
}
2021-04-30 15:22:29 +00:00
switch (audioPlayer.state) {
2021-06-06 08:50:26 +00:00
case PlayerState.PLAYING:
2021-04-30 15:22:29 +00:00
await audioPlayer.pause();
2020-03-15 10:27:51 +00:00
break;
2021-06-06 08:50:26 +00:00
case PlayerState.PAUSED:
2021-04-30 15:22:29 +00:00
await audioPlayer.resume();
2020-03-15 10:27:51 +00:00
break;
2021-06-06 08:50:26 +00:00
case PlayerState.STOPPED:
default:
2021-04-30 15:22:29 +00:00
onAudioPositionChanged ??=
audioPlayer.onAudioPositionChanged.listen((state) {
2021-05-01 13:45:48 +00:00
setState(() {
statusText =
'${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}';
currentPosition = state.inMilliseconds.toDouble();
});
2020-03-15 10:27:51 +00:00
});
2021-04-30 15:22:29 +00:00
onDurationChanged ??= audioPlayer.onDurationChanged.listen((max) =>
setState(() => maxPosition = max.inMilliseconds.toDouble()));
onPlayerStateChanged ??= audioPlayer.onPlayerStateChanged
.listen((_) => setState(() => null));
onPlayerError ??= audioPlayer.onPlayerError.listen((e) {
2021-05-23 11:11:55 +00:00
ScaffoldMessenger.of(context).showSnackBar(
2021-04-30 15:22:29 +00:00
SnackBar(
content: Text(L10n.of(context).oopsSomethingWentWrong),
),
);
SentryController.captureException(e, StackTrace.current);
});
await audioPlayer.play(audioFile.path);
2020-03-15 10:27:51 +00:00
break;
}
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
2021-10-14 16:09:30 +00:00
SizedBox(
2020-03-15 10:47:35 +00:00
width: 30,
2021-04-14 08:37:15 +00:00
child: status == AudioPlayerStatus.downloading
2021-10-14 16:09:30 +00:00
? const CircularProgressIndicator.adaptive(strokeWidth: 2)
2020-03-15 10:27:51 +00:00
: IconButton(
icon: Icon(
2021-06-06 08:50:26 +00:00
audioPlayer.state == PlayerState.PLAYING
2021-01-23 10:20:31 +00:00
? Icons.pause_outlined
: Icons.play_arrow_outlined,
2020-03-15 10:27:51 +00:00
color: widget.color,
),
2021-06-06 08:50:26 +00:00
tooltip: audioPlayer.state == PlayerState.PLAYING
2021-02-13 12:27:44 +00:00
? L10n.of(context).audioPlayerPause
: L10n.of(context).audioPlayerPlay,
2020-03-15 10:27:51 +00:00
onPressed: () {
2021-04-14 08:37:15 +00:00
if (status == AudioPlayerStatus.downloaded) {
2020-03-15 10:27:51 +00:00
_playAction();
} else {
_downloadAction();
}
},
),
),
2020-03-15 10:47:35 +00:00
Expanded(
child: Slider(
activeColor: Theme.of(context).colorScheme.secondaryVariant,
inactiveColor: widget.color.withAlpha(64),
2020-03-15 10:47:35 +00:00
value: currentPosition,
2021-04-30 15:22:29 +00:00
onChanged: (double position) =>
audioPlayer.seek(Duration(milliseconds: position.toInt())),
2021-04-14 08:37:15 +00:00
max: status == AudioPlayerStatus.downloaded ? maxPosition : 0,
2020-03-15 10:47:35 +00:00
min: 0,
),
2020-03-15 10:27:51 +00:00
),
Text(
statusText,
style: TextStyle(
color: widget.color,
),
),
2021-10-14 16:09:30 +00:00
const SizedBox(width: 8),
IconButton(
icon: Icon(
Icons.download_outlined,
color: widget.color,
),
onPressed: () => widget.event.saveFile(context),
),
2020-03-15 10:27:51 +00:00
],
);
}
}