flutter_sound_web.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2018, 2019, 2020 Dooboolab.
  3. *
  4. * This file is part of Flutter-Sound.
  5. *
  6. * Flutter-Sound is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License version 3 (LGPL-V3), as published by
  8. * the Free Software Foundation.
  9. *
  10. * Flutter-Sound is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Flutter-Sound. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. import 'dart:async';
  19. import 'dart:html' as html;
  20. import 'package:meta/meta.dart';
  21. import 'package:flutter_sound_platform_interface/flutter_sound_recorder_platform_interface.dart';
  22. import 'package:flutter_web_plugins/flutter_web_plugins.dart';
  23. import 'package:flutter_sound_web/flutter_sound_player_web.dart';
  24. import 'package:flutter_sound_web/flutter_sound_recorder_web.dart';
  25. import 'package:flutter/foundation.dart';
  26. class ImportJsLibraryWeb {
  27. /// Injects the library by its [url]
  28. static Future<void> import(String url) {
  29. return _importJSLibraries([url]);
  30. }
  31. static html.ScriptElement _createScriptTag(String library) {
  32. final html.ScriptElement script = html.ScriptElement()
  33. ..type = "text/javascript"
  34. ..charset = "utf-8"
  35. ..async = true
  36. //..defer = true
  37. ..src = library;
  38. return script;
  39. }
  40. /// Injects a bunch of libraries in the <head> and returns a
  41. /// Future that resolves when all load.
  42. static Future<void> _importJSLibraries(List<String> libraries) {
  43. final List<Future<void>> loading = <Future<void>>[];
  44. final head = html.querySelector('head')!;
  45. libraries.forEach((String library) {
  46. if (!isImported(library)) {
  47. final scriptTag = _createScriptTag(library);
  48. head.children.add(scriptTag);
  49. loading.add(scriptTag.onLoad.first);
  50. }
  51. });
  52. return Future.wait(loading);
  53. }
  54. static bool _isLoaded(html.Element head, String url) {
  55. if (url.startsWith("./")) {
  56. url = url.replaceFirst("./", "");
  57. }
  58. for (var element in head.children) {
  59. if (element is html.ScriptElement) {
  60. if (element.src.endsWith(url)) {
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. static bool isImported(String url) {
  68. final html.Element head = html.querySelector('head')!;
  69. return _isLoaded(head, url);
  70. }
  71. }
  72. class ImportJsLibrary {
  73. static Future<void> import(String url) {
  74. if (kIsWeb)
  75. return ImportJsLibraryWeb.import(url);
  76. else
  77. return Future.value(null);
  78. }
  79. static bool isImported(String url) {
  80. if (kIsWeb) {
  81. return ImportJsLibraryWeb.isImported(url);
  82. } else {
  83. return false;
  84. }
  85. }
  86. static registerWith(dynamic _) {
  87. // useful for flutter registrar
  88. }
  89. }
  90. String _libraryUrl(String url, String pluginName) {
  91. if (url.startsWith("./")) {
  92. url = url.replaceFirst("./", "");
  93. return "./assets/packages/$pluginName/$url";
  94. }
  95. if (url.startsWith("assets/")) {
  96. return "./assets/packages/$pluginName/$url";
  97. } else {
  98. return url;
  99. }
  100. }
  101. void importJsLibrary({required String url, required String flutterPluginName}) {
  102. if (flutterPluginName == null) {
  103. ImportJsLibrary.import(url);
  104. } else {
  105. ImportJsLibrary.import(_libraryUrl(url, flutterPluginName));
  106. }
  107. }
  108. bool isJsLibraryImported(String url, {required String flutterPluginName}) {
  109. if (flutterPluginName == null) {
  110. return ImportJsLibrary.isImported(url);
  111. } else {
  112. return ImportJsLibrary.isImported(_libraryUrl(url, flutterPluginName));
  113. }
  114. }
  115. /// The web implementation of [FlutterSoundRecorderPlatform].
  116. ///
  117. /// This class implements the `package:FlutterSoundPlayerPlatform` functionality for the web.
  118. class FlutterSoundPlugin //extends FlutterSoundPlatform
  119. {
  120. /// Registers this class as the default instance of [FlutterSoundPlatform].
  121. static void registerWith(Registrar registrar)
  122. {
  123. FlutterSoundPlayerWeb.registerWith(registrar);
  124. FlutterSoundRecorderWeb.registerWith(registrar);
  125. importJsLibrary(url: "./howler/howler.js", flutterPluginName: "flutter_sound_web");
  126. importJsLibrary(url: "./src/flutter_sound.js", flutterPluginName: "flutter_sound_web");
  127. importJsLibrary(url: "./src/flutter_sound_player.js", flutterPluginName: "flutter_sound_web");
  128. importJsLibrary(url: "./src/flutter_sound_recorder.js", flutterPluginName: "flutter_sound_web");
  129. }
  130. }