2020-03-15 11:27:51 +01:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-04-03 13:09:20 +02:00
|
|
|
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
2020-03-15 11:27:51 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-08-22 11:20:15 +02:00
|
|
|
import 'package:fluffychat/components/message_download_content.dart';
|
2021-04-03 13:09:20 +02:00
|
|
|
|
2020-03-15 11:27:51 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-01-23 11:17:34 +01:00
|
|
|
import 'package:flutter_sound_lite/flutter_sound.dart';
|
2020-08-22 11:20:15 +02:00
|
|
|
import 'package:universal_html/prefer_universal/html.dart' as html;
|
2021-02-13 13:27:44 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-08-22 11:20:15 +02:00
|
|
|
import '../utils/ui_fake.dart' if (dart.library.html) 'dart:ui' as ui;
|
|
|
|
import 'matrix.dart';
|
2020-11-17 12:59:34 +01:00
|
|
|
import '../utils/event_extension.dart';
|
2020-03-15 11:27:51 +01:00
|
|
|
|
|
|
|
class AudioPlayer extends StatefulWidget {
|
|
|
|
final Color color;
|
2020-03-29 20:13:25 +02:00
|
|
|
final Event event;
|
2020-03-15 11:27:51 +01:00
|
|
|
|
2020-03-29 20:13:25 +02:00
|
|
|
static String currentId;
|
2020-03-15 14:15:45 +01:00
|
|
|
|
2020-03-29 20:13:25 +02:00
|
|
|
const AudioPlayer(this.event, {this.color = Colors.black, Key key})
|
2020-03-15 11:27:51 +01:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_AudioPlayerState createState() => _AudioPlayerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AudioPlayerStatus { NOT_DOWNLOADED, DOWNLOADING, DOWNLOADED }
|
|
|
|
|
|
|
|
class _AudioPlayerState extends State<AudioPlayer> {
|
|
|
|
AudioPlayerStatus status = AudioPlayerStatus.NOT_DOWNLOADED;
|
|
|
|
|
2021-01-23 11:17:34 +01:00
|
|
|
final FlutterSoundPlayer flutterSound = FlutterSoundPlayer();
|
2020-03-15 11:27:51 +01:00
|
|
|
|
|
|
|
StreamSubscription soundSubscription;
|
|
|
|
Uint8List audioFile;
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
String statusText = '00:00';
|
2020-03-15 11:27:51 +01:00
|
|
|
double currentPosition = 0;
|
|
|
|
double maxPosition = 0;
|
|
|
|
|
2020-08-22 11:20:15 +02:00
|
|
|
String webSrcUrl;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
if (kIsWeb) {
|
|
|
|
ui.platformViewRegistry.registerViewFactory(
|
|
|
|
'web_audio_player',
|
|
|
|
(int viewId) => html.AudioElement()
|
|
|
|
..src = webSrcUrl
|
|
|
|
..autoplay = false
|
|
|
|
..controls = true
|
|
|
|
..style.border = 'none');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 11:27:51 +01:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2021-01-23 11:17:34 +01:00
|
|
|
if (flutterSound.isPlaying) {
|
2020-03-15 11:27:51 +01:00
|
|
|
flutterSound.stopPlayer();
|
|
|
|
}
|
2021-01-23 11:17:34 +01:00
|
|
|
if (flutterSound.isOpen()) {
|
|
|
|
flutterSound.closeAudioSession();
|
|
|
|
}
|
2020-03-15 11:27:51 +01:00
|
|
|
soundSubscription?.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
Future<void> _downloadAction() async {
|
2020-03-15 11:27:51 +01:00
|
|
|
if (status != AudioPlayerStatus.NOT_DOWNLOADED) return;
|
|
|
|
setState(() => status = AudioPlayerStatus.DOWNLOADING);
|
2020-12-25 09:58:34 +01:00
|
|
|
try {
|
|
|
|
final matrixFile =
|
|
|
|
await widget.event.downloadAndDecryptAttachmentCached();
|
|
|
|
setState(() {
|
|
|
|
audioFile = matrixFile.bytes;
|
|
|
|
status = AudioPlayerStatus.DOWNLOADED;
|
|
|
|
});
|
|
|
|
_playAction();
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().v('Could not download audio file', e, s);
|
2021-04-03 13:09:20 +02:00
|
|
|
AdaptivePageLayout.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(e.toLocalizedString(context)),
|
|
|
|
),
|
|
|
|
);
|
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 {
|
2020-03-29 20:13:25 +02:00
|
|
|
if (AudioPlayer.currentId != widget.event.eventId) {
|
|
|
|
if (AudioPlayer.currentId != null) {
|
2021-01-23 11:17:34 +01:00
|
|
|
if (!flutterSound.isStopped) {
|
2020-03-15 12:00:25 +01:00
|
|
|
await flutterSound.stopPlayer();
|
2020-03-15 17:12:16 +01:00
|
|
|
setState(() => null);
|
2020-03-15 12:00:25 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-29 20:13:25 +02:00
|
|
|
AudioPlayer.currentId = widget.event.eventId;
|
2020-03-15 12:00:25 +01:00
|
|
|
}
|
2021-01-23 11:17:34 +01:00
|
|
|
switch (flutterSound.playerState) {
|
|
|
|
case PlayerState.isPlaying:
|
2020-03-15 11:27:51 +01:00
|
|
|
await flutterSound.pausePlayer();
|
|
|
|
break;
|
2021-01-23 11:17:34 +01:00
|
|
|
case PlayerState.isPaused:
|
2020-03-15 11:27:51 +01:00
|
|
|
await flutterSound.resumePlayer();
|
|
|
|
break;
|
2021-01-23 11:17:34 +01:00
|
|
|
case PlayerState.isStopped:
|
|
|
|
default:
|
|
|
|
if (!flutterSound.isOpen()) {
|
|
|
|
await flutterSound.openAudioSession(
|
|
|
|
focus: AudioFocus.requestFocusAndStopOthers,
|
|
|
|
category: SessionCategory.playback);
|
|
|
|
}
|
|
|
|
|
|
|
|
await flutterSound.setSubscriptionDuration(Duration(milliseconds: 100));
|
|
|
|
await flutterSound.startPlayer(fromDataBuffer: audioFile);
|
|
|
|
soundSubscription ??= flutterSound.onProgress.listen((e) {
|
2020-03-29 20:13:25 +02:00
|
|
|
if (AudioPlayer.currentId != widget.event.eventId) {
|
2020-03-15 12:00:25 +01:00
|
|
|
soundSubscription?.cancel()?.then((f) => soundSubscription = null);
|
2020-05-13 15:58:59 +02:00
|
|
|
setState(() {
|
2020-03-15 12:00:25 +01:00
|
|
|
currentPosition = 0;
|
2020-05-13 15:58:59 +02:00
|
|
|
statusText = '00:00';
|
2020-03-15 12:00:25 +01:00
|
|
|
});
|
2020-03-29 20:13:25 +02:00
|
|
|
AudioPlayer.currentId = null;
|
2020-03-15 12:00:25 +01:00
|
|
|
} else if (e != null) {
|
2021-01-23 11:17:34 +01:00
|
|
|
var txt =
|
|
|
|
'${e.position.inMinutes.toString().padLeft(2, '0')}:${(e.position.inSeconds % 60).toString().padLeft(2, '0')}';
|
2020-05-13 15:58:59 +02:00
|
|
|
setState(() {
|
2021-01-23 11:17:34 +01:00
|
|
|
maxPosition = e.duration.inMilliseconds.toDouble();
|
|
|
|
currentPosition = e.position.inMilliseconds.toDouble();
|
2020-03-15 11:27:51 +01:00
|
|
|
statusText = txt;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-08-22 11:20:15 +02:00
|
|
|
if (kIsWeb) {
|
|
|
|
if (widget.event.content['url'] is String) {
|
|
|
|
webSrcUrl = Uri.parse(widget.event.content['url'])
|
|
|
|
.getDownloadLink(Matrix.of(context).client);
|
|
|
|
return Container(
|
|
|
|
height: 50,
|
|
|
|
width: 300,
|
|
|
|
child: HtmlElementView(viewType: 'web_audio_player'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return MessageDownloadContent(widget.event, widget.color);
|
|
|
|
}
|
2020-03-15 11:27:51 +01:00
|
|
|
return Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
2020-03-15 11:47:35 +01:00
|
|
|
width: 30,
|
2020-03-15 11:27:51 +01:00
|
|
|
child: status == AudioPlayerStatus.DOWNLOADING
|
2020-03-15 11:47:35 +01:00
|
|
|
? CircularProgressIndicator(strokeWidth: 2)
|
2020-03-15 11:27:51 +01:00
|
|
|
: IconButton(
|
|
|
|
icon: Icon(
|
2021-01-23 11:20:31 +01:00
|
|
|
flutterSound.isPlaying
|
|
|
|
? Icons.pause_outlined
|
|
|
|
: Icons.play_arrow_outlined,
|
2020-03-15 11:27:51 +01:00
|
|
|
color: widget.color,
|
|
|
|
),
|
2021-02-13 13:27:44 +01:00
|
|
|
tooltip: flutterSound.isPlaying
|
|
|
|
? L10n.of(context).audioPlayerPause
|
|
|
|
: L10n.of(context).audioPlayerPlay,
|
2020-03-15 11:27:51 +01:00
|
|
|
onPressed: () {
|
|
|
|
if (status == AudioPlayerStatus.DOWNLOADED) {
|
|
|
|
_playAction();
|
|
|
|
} else {
|
|
|
|
_downloadAction();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2020-03-15 11:47:35 +01:00
|
|
|
Expanded(
|
|
|
|
child: Slider(
|
|
|
|
value: currentPosition,
|
2021-01-23 11:17:34 +01:00
|
|
|
onChanged: (double position) => flutterSound
|
|
|
|
.seekToPlayer(Duration(milliseconds: position.toInt())),
|
2020-03-15 11:47:35 +01:00
|
|
|
max: status == AudioPlayerStatus.DOWNLOADED ? maxPosition : 0,
|
|
|
|
min: 0,
|
|
|
|
),
|
2020-03-15 11:27:51 +01:00
|
|
|
),
|
|
|
|
Text(
|
|
|
|
statusText,
|
|
|
|
style: TextStyle(
|
|
|
|
color: widget.color,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|