audio_service_web.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import 'dart:async';
  2. import 'dart:html' as html;
  3. import 'dart:js' as js;
  4. import 'package:audio_service/js/media_metadata.dart';
  5. import 'js/media_session_web.dart';
  6. import 'package:audio_service/audio_service.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:flutter_web_plugins/flutter_web_plugins.dart';
  9. const String _CUSTOM_PREFIX = 'custom_';
  10. class Art {
  11. String src;
  12. String type;
  13. String sizes;
  14. Art({this.src, this.type, this.sizes});
  15. }
  16. class AudioServicePlugin {
  17. int fastForwardInterval;
  18. int rewindInterval;
  19. Map params;
  20. bool started = false;
  21. ClientHandler clientHandler;
  22. BackgroundHandler backgroundHandler;
  23. static void registerWith(Registrar registrar) {
  24. AudioServicePlugin(registrar);
  25. }
  26. AudioServicePlugin(Registrar registrar) {
  27. clientHandler = ClientHandler(this, registrar);
  28. backgroundHandler = BackgroundHandler(this, registrar);
  29. }
  30. }
  31. class ClientHandler {
  32. final AudioServicePlugin plugin;
  33. final MethodChannel channel;
  34. ClientHandler(this.plugin, Registrar registrar)
  35. : channel = MethodChannel(
  36. 'ryanheise.com/audioService',
  37. const StandardMethodCodec(),
  38. registrar.messenger,
  39. ) {
  40. channel.setMethodCallHandler(handleServiceMethodCall);
  41. }
  42. Future<T> invokeMethod<T>(String method, [dynamic arguments]) =>
  43. channel.invokeMethod(method, arguments);
  44. Future<dynamic> handleServiceMethodCall(MethodCall call) async {
  45. switch (call.method) {
  46. case 'start':
  47. plugin.fastForwardInterval = call.arguments['fastForwardInterval'];
  48. plugin.rewindInterval = call.arguments['rewindInterval'];
  49. plugin.params = call.arguments['params'];
  50. plugin.started = true;
  51. return plugin.started;
  52. case 'connect':
  53. // No-op not really anything for us to do with connect on the web, the
  54. // streams should all be hydrated
  55. break;
  56. case 'disconnect':
  57. // No-op not really anything for us to do with disconnect on the web,
  58. // the streams should stay hydrated because everything is static and we
  59. // aren't working with isolates
  60. break;
  61. case 'isRunning':
  62. return plugin.started;
  63. case 'rewind':
  64. return plugin.backgroundHandler.invokeMethod('onRewind');
  65. case 'fastForward':
  66. return plugin.backgroundHandler.invokeMethod('onFastForward');
  67. case 'skipToPrevious':
  68. return plugin.backgroundHandler.invokeMethod('onSkipToPrevious');
  69. case 'skipToNext':
  70. return plugin.backgroundHandler.invokeMethod('onSkipToNext');
  71. case 'play':
  72. return plugin.backgroundHandler.invokeMethod('onPlay');
  73. case 'pause':
  74. return plugin.backgroundHandler.invokeMethod('onPause');
  75. case 'stop':
  76. return plugin.backgroundHandler.invokeMethod('onStop');
  77. case 'seekTo':
  78. return plugin.backgroundHandler
  79. .invokeMethod('onSeekTo', [call.arguments]);
  80. case 'prepareFromMediaId':
  81. return plugin.backgroundHandler
  82. .invokeMethod('onPrepareFromMediaId', [call.arguments]);
  83. case 'playFromMediaId':
  84. return plugin.backgroundHandler
  85. .invokeMethod('onPlayFromMediaId', [call.arguments]);
  86. case 'setBrowseMediaParent':
  87. return plugin.backgroundHandler
  88. .invokeMethod('onLoadChildren', [call.arguments]);
  89. case 'onClick':
  90. // No-op we don't really have the idea of a bluetooth button click on
  91. // the web
  92. break;
  93. case 'addQueueItem':
  94. return plugin.backgroundHandler
  95. .invokeMethod('onAddQueueItem', [call.arguments]);
  96. case 'addQueueItemAt':
  97. return plugin.backgroundHandler
  98. .invokeMethod('onQueueItemAt', call.arguments);
  99. case 'removeQueueItem':
  100. return plugin.backgroundHandler
  101. .invokeMethod('onRemoveQueueItem', [call.arguments]);
  102. case 'updateQueue':
  103. return plugin.backgroundHandler
  104. .invokeMethod('onUpdateQueue', [call.arguments]);
  105. case 'updateMediaItem':
  106. return plugin.backgroundHandler
  107. .invokeMethod('onUpdateMediaItem', [call.arguments]);
  108. case 'prepare':
  109. return plugin.backgroundHandler.invokeMethod('onPrepare');
  110. case 'playMediaItem':
  111. return plugin.backgroundHandler
  112. .invokeMethod('onPlayMediaItem', [call.arguments]);
  113. case 'skipToQueueItem':
  114. return plugin.backgroundHandler
  115. .invokeMethod('onSkipToMediaItem', [call.arguments]);
  116. case 'setRepeatMode':
  117. return plugin.backgroundHandler
  118. .invokeMethod('onSetRepeatMode', [call.arguments]);
  119. case 'setShuffleMode':
  120. return plugin.backgroundHandler
  121. .invokeMethod('onSetShuffleMode', [call.arguments]);
  122. case 'setRating':
  123. return plugin.backgroundHandler.invokeMethod('onSetRating',
  124. [call.arguments['rating'], call.arguments['extras']]);
  125. case 'setSpeed':
  126. return plugin.backgroundHandler
  127. .invokeMethod('onSetSpeed', [call.arguments]);
  128. default:
  129. if (call.method.startsWith(_CUSTOM_PREFIX)) {
  130. final result = await plugin.backgroundHandler
  131. .invokeMethod(call.method, call.arguments);
  132. return result;
  133. }
  134. throw PlatformException(
  135. code: 'Unimplemented',
  136. details: "The audio Service plugin for web doesn't implement "
  137. "the method '${call.method}'");
  138. }
  139. }
  140. }
  141. class BackgroundHandler {
  142. final AudioServicePlugin plugin;
  143. final MethodChannel channel;
  144. MediaItem mediaItem;
  145. BackgroundHandler(this.plugin, Registrar registrar)
  146. : channel = MethodChannel(
  147. 'ryanheise.com/audioServiceBackground',
  148. const StandardMethodCodec(),
  149. registrar.messenger,
  150. ) {
  151. channel.setMethodCallHandler(handleBackgroundMethodCall);
  152. }
  153. Future<T> invokeMethod<T>(String method, [dynamic arguments]) =>
  154. channel.invokeMethod(method, arguments);
  155. Future<dynamic> handleBackgroundMethodCall(MethodCall call) async {
  156. switch (call.method) {
  157. case 'started':
  158. return started(call);
  159. case 'ready':
  160. return ready(call);
  161. case 'stopped':
  162. return stopped(call);
  163. case 'setState':
  164. return setState(call);
  165. case 'setMediaItem':
  166. return setMediaItem(call);
  167. case 'setQueue':
  168. return setQueue(call);
  169. case 'androidForceEnableMediaButtons':
  170. //no-op
  171. break;
  172. default:
  173. throw PlatformException(
  174. code: 'Unimplemented',
  175. details:
  176. "The audio service background plugin for web doesn't implement "
  177. "the method '${call.method}'");
  178. }
  179. }
  180. Future<bool> started(MethodCall call) async => true;
  181. Future<dynamic> ready(MethodCall call) async => {
  182. 'fastForwardInterval': plugin.fastForwardInterval ?? 30000,
  183. 'rewindInterval': plugin.rewindInterval ?? 30000,
  184. 'params': plugin.params
  185. };
  186. Future<void> stopped(MethodCall call) async {
  187. final session = html.window.navigator.mediaSession;
  188. session.metadata = null;
  189. plugin.started = false;
  190. mediaItem = null;
  191. plugin.clientHandler.invokeMethod('onStopped');
  192. }
  193. Future<void> setState(MethodCall call) async {
  194. final session = html.window.navigator.mediaSession;
  195. final List args = call.arguments;
  196. final List<MediaControl> controls = call.arguments[0]
  197. .map<MediaControl>((element) => MediaControl(
  198. action: MediaAction.values[element['action']],
  199. androidIcon: element['androidIcon'],
  200. label: element['label']))
  201. .toList();
  202. // Reset the handlers
  203. // TODO: Make this better... Like only change ones that have been changed
  204. try {
  205. session.setActionHandler('play', null);
  206. session.setActionHandler('pause', null);
  207. session.setActionHandler('previoustrack', null);
  208. session.setActionHandler('nexttrack', null);
  209. session.setActionHandler('seekbackward', null);
  210. session.setActionHandler('seekforward', null);
  211. session.setActionHandler('stop', null);
  212. } catch (e) {}
  213. int actionBits = 0;
  214. for (final control in controls) {
  215. try {
  216. switch (control.action) {
  217. case MediaAction.play:
  218. session.setActionHandler('play', AudioService.play);
  219. break;
  220. case MediaAction.pause:
  221. session.setActionHandler('pause', AudioService.pause);
  222. break;
  223. case MediaAction.skipToPrevious:
  224. session.setActionHandler(
  225. 'previoustrack', AudioService.skipToPrevious);
  226. break;
  227. case MediaAction.skipToNext:
  228. session.setActionHandler('nexttrack', AudioService.skipToNext);
  229. break;
  230. // The naming convention here is a bit odd but seekbackward seems more
  231. // analagous to rewind than seekBackward
  232. case MediaAction.rewind:
  233. session.setActionHandler('seekbackward', AudioService.rewind);
  234. break;
  235. case MediaAction.fastForward:
  236. session.setActionHandler('seekforward', AudioService.fastForward);
  237. break;
  238. case MediaAction.stop:
  239. session.setActionHandler('stop', AudioService.stop);
  240. break;
  241. default:
  242. // no-op
  243. break;
  244. }
  245. } catch (e) {}
  246. int actionCode = 1 << control.action.index;
  247. actionBits |= actionCode;
  248. }
  249. for (int rawSystemAction in call.arguments[1]) {
  250. MediaAction action = MediaAction.values[rawSystemAction];
  251. switch (action) {
  252. case MediaAction.seekTo:
  253. try {
  254. setActionHandler('seekto', js.allowInterop((ActionResult ev) {
  255. print(ev.action);
  256. print(ev.seekTime);
  257. // Chrome uses seconds for whatever reason
  258. AudioService.seekTo(Duration(
  259. milliseconds: (ev.seekTime * 1000).round(),
  260. ));
  261. }));
  262. } catch (e) {}
  263. break;
  264. default:
  265. // no-op
  266. break;
  267. }
  268. int actionCode = 1 << rawSystemAction;
  269. actionBits |= actionCode;
  270. }
  271. try {
  272. // Dart also doesn't expose setPositionState
  273. if (mediaItem != null) {
  274. print(
  275. 'Setting positionState Duration(${mediaItem.duration.inSeconds}), PlaybackRate(${args[6] ?? 1.0}), Position(${Duration(milliseconds: args[4]).inSeconds})');
  276. // Chrome looks for seconds for some reason
  277. setPositionState(PositionState(
  278. duration: (mediaItem.duration?.inMilliseconds ?? 0) / 1000,
  279. playbackRate: args[6] ?? 1.0,
  280. position: (args[4] ?? 0) / 1000,
  281. ));
  282. }
  283. } catch (e) {
  284. print(e);
  285. }
  286. plugin.clientHandler.invokeMethod('onPlaybackStateChanged', [
  287. args[2], // Processing state
  288. args[3], // Playing
  289. actionBits, // Action bits
  290. args[4], // Position
  291. args[5], // bufferedPosition
  292. args[6] ?? 1.0, // speed
  293. args[7] ?? DateTime.now().millisecondsSinceEpoch, // updateTime
  294. args[9], // repeatMode
  295. args[10], // shuffleMode
  296. ]);
  297. }
  298. Future<void> setMediaItem(MethodCall call) async {
  299. mediaItem = MediaItem.fromJson(call.arguments);
  300. // This would be how we could pull images out of the cache... But nothing is actually cached on web
  301. final artUri = /* mediaItem.extras['artCacheFile'] ?? */
  302. mediaItem.artUri;
  303. try {
  304. metadata = MediaMetadata(MetadataLiteral(
  305. album: mediaItem.album,
  306. title: mediaItem.title,
  307. artist: mediaItem.artist,
  308. artwork: [
  309. MetadataArtwork(
  310. src: artUri,
  311. sizes: '512x512',
  312. )
  313. ],
  314. ));
  315. } catch (e) {
  316. print('Metadata failed $e');
  317. }
  318. plugin.clientHandler.invokeMethod('onMediaChanged', [mediaItem.toJson()]);
  319. }
  320. Future<void> setQueue(MethodCall call) async {
  321. plugin.clientHandler.invokeMethod('onQueueChanged', [call.arguments]);
  322. }
  323. }