From 0f6b46dd82f3b51a3ea538eee0d5ae5169d62f85 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 22 Aug 2020 11:20:15 +0200 Subject: [PATCH] feat: Implement web audio player --- lib/components/audio_player.dart | 33 +++++++++++++++++++++++++++++++- lib/utils/ui_fake.dart | 4 ++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/utils/ui_fake.dart diff --git a/lib/components/audio_player.dart b/lib/components/audio_player.dart index 0ffb4bf..940fa0f 100644 --- a/lib/components/audio_player.dart +++ b/lib/components/audio_player.dart @@ -2,12 +2,15 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:famedlysdk/famedlysdk.dart'; +import 'package:fluffychat/components/message_download_content.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_sound/flutter_sound.dart'; import 'package:intl/intl.dart'; - +import 'package:universal_html/prefer_universal/html.dart' as html; import 'dialogs/simple_dialogs.dart'; +import '../utils/ui_fake.dart' if (dart.library.html) 'dart:ui' as ui; +import 'matrix.dart'; class AudioPlayer extends StatefulWidget { final Color color; @@ -36,6 +39,22 @@ class _AudioPlayerState extends State { double currentPosition = 0; double maxPosition = 0; + 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'); + } + } + @override void dispose() { if (flutterSound.audioState == t_AUDIO_STATE.IS_PLAYING) { @@ -111,6 +130,18 @@ class _AudioPlayerState extends State { @override Widget build(BuildContext context) { + 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); + } return Row( mainAxisSize: MainAxisSize.min, children: [ diff --git a/lib/utils/ui_fake.dart b/lib/utils/ui_fake.dart new file mode 100644 index 0000000..46c8a74 --- /dev/null +++ b/lib/utils/ui_fake.dart @@ -0,0 +1,4 @@ +// ignore: camel_case_types +class platformViewRegistry { + static void registerViewFactory(String viewId, dynamic cb) {} +}