fix: Audioplayer on web buggy
This commit is contained in:
parent
7de276f071
commit
402801c458
1 changed files with 8 additions and 25 deletions
|
|
@ -10,6 +10,7 @@ import 'package:just_audio/just_audio.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
import 'package:opus_caf_converter_dart/opus_caf_converter_dart.dart';
|
import 'package:opus_caf_converter_dart/opus_caf_converter_dart.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
import 'package:universal_html/html.dart' as html;
|
||||||
|
|
||||||
import 'package:fluffychat/config/app_config.dart';
|
import 'package:fluffychat/config/app_config.dart';
|
||||||
import 'package:fluffychat/config/themes.dart';
|
import 'package:fluffychat/config/themes.dart';
|
||||||
|
|
@ -131,10 +132,8 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
matrix.voiceMessageEventId.value != widget.event.eventId
|
matrix.voiceMessageEventId.value != widget.event.eventId
|
||||||
? null
|
? null
|
||||||
: matrix.audioPlayer;
|
: matrix.audioPlayer;
|
||||||
if (currentPlayer != null) {
|
if (currentPlayer != null && !currentPlayer.isAtEndPosition) {
|
||||||
if (currentPlayer.isAtEndPosition) {
|
if (currentPlayer.playing) {
|
||||||
currentPlayer.seek(Duration.zero);
|
|
||||||
} else if (currentPlayer.playing) {
|
|
||||||
currentPlayer.pause();
|
currentPlayer.pause();
|
||||||
} else {
|
} else {
|
||||||
currentPlayer.play();
|
currentPlayer.play();
|
||||||
|
|
@ -204,8 +203,12 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
|
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
audioPlayer.setFilePath(file.path);
|
audioPlayer.setFilePath(file.path);
|
||||||
|
} else if (kIsWeb) {
|
||||||
|
final blob = html.Blob([matrixFile.bytes], 'audio/mpeg');
|
||||||
|
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||||
|
await audioPlayer.setAudioSource(AudioSource.uri(Uri.parse(url)));
|
||||||
} else {
|
} else {
|
||||||
await audioPlayer.setAudioSource(MatrixFileAudioSource(matrixFile));
|
throw Exception('No audio file provided!');
|
||||||
}
|
}
|
||||||
|
|
||||||
audioPlayer.play().onError(
|
audioPlayer.play().onError(
|
||||||
|
|
@ -513,26 +516,6 @@ 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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension on AudioPlayer {
|
extension on AudioPlayer {
|
||||||
bool get isAtEndPosition {
|
bool get isAtEndPosition {
|
||||||
final duration = this.duration;
|
final duration = this.duration;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue