feat: Enable audioplayer for web and linux

This commit is contained in:
Christian Pauly 2023-02-14 09:15:47 +01:00
parent 4345ab9772
commit 69e0c48c8e
2 changed files with 26 additions and 17 deletions

View File

@ -1,12 +1,10 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart'; import 'package:fluffychat/utils/localized_exception_extension.dart';
@ -42,7 +40,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
int currentPosition = 0; int currentPosition = 0;
double maxPosition = 0; double maxPosition = 0;
File? audioFile; MatrixFile? audioFile;
@override @override
void dispose() { void dispose() {
@ -62,14 +60,8 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
setState(() => status = AudioPlayerStatus.downloading); setState(() => status = AudioPlayerStatus.downloading);
try { try {
final matrixFile = await widget.event.downloadAndDecryptAttachment(); final matrixFile = await widget.event.downloadAndDecryptAttachment();
final tempDir = await getTemporaryDirectory();
final fileName = Uri.encodeComponent(
widget.event.attachmentOrThumbnailMxcUrl()!.pathSegments.last);
final file = File('${tempDir.path}/${fileName}_${matrixFile.name}');
await file.writeAsBytes(matrixFile.bytes);
setState(() { setState(() {
audioFile = file; audioFile = matrixFile;
status = AudioPlayerStatus.downloaded; status = AudioPlayerStatus.downloaded;
}); });
_playAction(); _playAction();
@ -117,7 +109,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
}); });
onPlayerStateChanged ??= onPlayerStateChanged ??=
audioPlayer.playingStream.listen((_) => setState(() {})); audioPlayer.playingStream.listen((_) => setState(() {}));
audioPlayer.setFilePath(audioFile!.path); audioPlayer.setAudioSource(MatrixFileAudioSource(audioFile!));
audioPlayer.play().catchError((e, s) { audioPlayer.play().catchError((e, s) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
@ -251,3 +243,22 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
); );
} }
} }
/// 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,
);
}
}

View File

@ -113,12 +113,10 @@ class MessageContent extends StatelessWidget {
case CuteEventContent.eventType: case CuteEventContent.eventType:
return CuteContent(event); return CuteContent(event);
case MessageTypes.Audio: case MessageTypes.Audio:
if (PlatformInfos.isMobile || PlatformInfos.isMacOS if (PlatformInfos.isMobile ||
// || latformInfos.isLinux PlatformInfos.isMacOS ||
// disabled until PlatformInfos.isWeb ||
// https://github.com/bleonard252/just_audio_mpv/issues/3 is PlatformInfos.isLinux) {
// fixed
) {
return AudioPlayerWidget( return AudioPlayerWidget(
event, event,
color: textColor, color: textColor,