From b646257e5354efe23a553f6b84fed3be139b0cf9 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Sat, 18 Mar 2023 22:14:53 +0100 Subject: [PATCH 1/2] feat!: improve separation of global audioplayer interface --- .../example/integration_test/lib_test.dart | 8 +++--- .../example/lib/tabs/audio_context.dart | 4 +-- .../audioplayers/example/lib/tabs/logger.dart | 2 +- packages/audioplayers/lib/audioplayers.dart | 2 +- .../audioplayers/lib/src/audioplayer.dart | 4 +-- .../lib/src/global_audioplayer.dart | 24 ++++++++++++++++++ .../lib/audioplayers_platform_interface.dart | 2 +- ...atform.dart => audioplayers_platform.dart} | 11 ++++---- .../src/audioplayers_platform_interface.dart | 17 +++++++------ ...dart => global_audioplayers_platform.dart} | 24 ++---------------- ...lobal_audioplayers_platform_interface.dart | 25 +++++++++++++++++++ .../test/logger_test.dart | 2 +- .../lib/audioplayers_web.dart | 10 ++++++-- .../lib/global_audioplayers_web.dart | 22 ++++++++++++++++ 14 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 packages/audioplayers/lib/src/global_audioplayer.dart rename packages/audioplayers_platform_interface/lib/src/{method_channel_audioplayers_platform.dart => audioplayers_platform.dart} (94%) rename packages/audioplayers_platform_interface/lib/src/{global_platform_interface.dart => global_audioplayers_platform.dart} (57%) create mode 100644 packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform_interface.dart create mode 100644 packages/audioplayers_web/lib/global_audioplayers_web.dart diff --git a/packages/audioplayers/example/integration_test/lib_test.dart b/packages/audioplayers/example/integration_test/lib_test.dart index e5bed6738..298fbd99d 100644 --- a/packages/audioplayers/example/integration_test/lib_test.dart +++ b/packages/audioplayers/example/integration_test/lib_test.dart @@ -131,7 +131,7 @@ void main() { //ignore: avoid_redundant_argument_values respectSilence: false, ).build(); - await AudioPlayer.global.setGlobalAudioContext(audioContext); + await AudioPlayer.global.setAudioContext(audioContext); await player.setAudioContext(audioContext); await player.play(td.source); @@ -143,7 +143,7 @@ void main() { forceSpeaker: false, respectSilence: true, ).build(); - await AudioPlayer.global.setGlobalAudioContext(audioContext); + await AudioPlayer.global.setAudioContext(audioContext); await player.setAudioContext(audioContext); await player.resume(); @@ -172,7 +172,7 @@ void main() { //ignore: avoid_redundant_argument_values respectSilence: false, ).build(); - await AudioPlayer.global.setGlobalAudioContext(audioContext); + await AudioPlayer.global.setAudioContext(audioContext); await player.setAudioContext(audioContext); await player.setSource(td.source); @@ -187,7 +187,7 @@ void main() { forceSpeaker: false, respectSilence: true, ).build(); - await AudioPlayer.global.setGlobalAudioContext(audioContext); + await AudioPlayer.global.setAudioContext(audioContext); await player.setAudioContext(audioContext); await player.resume(); diff --git a/packages/audioplayers/example/lib/tabs/audio_context.dart b/packages/audioplayers/example/lib/tabs/audio_context.dart index 9935db066..62f1e3fca 100644 --- a/packages/audioplayers/example/lib/tabs/audio_context.dart +++ b/packages/audioplayers/example/lib/tabs/audio_context.dart @@ -16,7 +16,7 @@ class AudioContextTab extends StatefulWidget { class _AudioContextTabState extends State with AutomaticKeepAliveClientMixin { - static GlobalPlatformInterface get _global => AudioPlayer.global; + static GlobalAudioPlayer get _global => AudioPlayer.global; AudioPlayer get player => widget.player; @@ -43,7 +43,7 @@ class _AudioContextTabState extends State ElevatedButton.icon( icon: const Icon(Icons.public), label: const Text('Global'), - onPressed: () => _global.setGlobalAudioContext(audioContext), + onPressed: () => _global.setAudioContext(audioContext), ), ElevatedButton.icon( icon: const Icon(Icons.looks_one), diff --git a/packages/audioplayers/example/lib/tabs/logger.dart b/packages/audioplayers/example/lib/tabs/logger.dart index c2d1b0460..29a5f4c5a 100644 --- a/packages/audioplayers/example/lib/tabs/logger.dart +++ b/packages/audioplayers/example/lib/tabs/logger.dart @@ -11,7 +11,7 @@ class LoggerTab extends StatefulWidget { } class _LoggerTabState extends State { - static GlobalPlatformInterface get _logger => AudioPlayer.global; + static GlobalAudioPlayer get _logger => AudioPlayer.global; LogLevel currentLogLevel = _logger.logLevel; diff --git a/packages/audioplayers/lib/audioplayers.dart b/packages/audioplayers/lib/audioplayers.dart index 4272513ec..6cf1f29ef 100644 --- a/packages/audioplayers/lib/audioplayers.dart +++ b/packages/audioplayers/lib/audioplayers.dart @@ -5,9 +5,9 @@ export 'package:audioplayers_platform_interface/src/api/log_level.dart'; export 'package:audioplayers_platform_interface/src/api/player_mode.dart'; export 'package:audioplayers_platform_interface/src/api/player_state.dart'; export 'package:audioplayers_platform_interface/src/api/release_mode.dart'; -export 'package:audioplayers_platform_interface/src/global_platform_interface.dart'; export 'src/audio_cache.dart'; export 'src/audio_pool.dart'; export 'src/audioplayer.dart'; +export 'src/global_audioplayer.dart'; export 'src/source.dart'; diff --git a/packages/audioplayers/lib/src/audioplayer.dart b/packages/audioplayers/lib/src/audioplayer.dart index 6a2bf41fe..fa0c5ac5e 100644 --- a/packages/audioplayers/lib/src/audioplayer.dart +++ b/packages/audioplayers/lib/src/audioplayer.dart @@ -18,8 +18,8 @@ const _uuid = Uuid(); /// It holds methods to play, loop, pause, stop, seek the audio, and some useful /// hooks for handlers and callbacks. class AudioPlayer { - static final global = GlobalPlatformInterface.instance; - static final _platform = AudioplayersPlatform.instance; + static final global = GlobalAudioPlayer(); + static final _platform = AudioplayersPlatformInterface.instance; /// This is the [AudioCache] instance used by this player. /// Unless you want to control multiple caches separately, you don't need to diff --git a/packages/audioplayers/lib/src/global_audioplayer.dart b/packages/audioplayers/lib/src/global_audioplayer.dart new file mode 100644 index 000000000..f7f9295c2 --- /dev/null +++ b/packages/audioplayers/lib/src/global_audioplayer.dart @@ -0,0 +1,24 @@ +import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart'; + +/// Handle Global calls and events concerning all AudioPlayers. +class GlobalAudioPlayer { + static final _platform = GlobalAudioplayersPlatformInterface.instance; + + LogLevel get logLevel => _platform.logLevel; + + Future changeLogLevel(LogLevel level) => + _platform.changeLogLevel(level); + + void log(LogLevel level, String message) => _platform.log(level, message); + + void info(String message) => _platform.info(message); + + void error(String message) => _platform.error(message); + + Future setAudioContext(AudioContext ctx) => + _platform.setGlobalAudioContext(ctx); + + @Deprecated('Use `setAudioContext()` instead.') + Future setGlobalAudioContext(AudioContext ctx) => + _platform.setGlobalAudioContext(ctx); +} diff --git a/packages/audioplayers_platform_interface/lib/audioplayers_platform_interface.dart b/packages/audioplayers_platform_interface/lib/audioplayers_platform_interface.dart index 18bf7fcd0..939b7f92c 100644 --- a/packages/audioplayers_platform_interface/lib/audioplayers_platform_interface.dart +++ b/packages/audioplayers_platform_interface/lib/audioplayers_platform_interface.dart @@ -5,5 +5,5 @@ export 'src/api/player_mode.dart'; export 'src/api/player_state.dart'; export 'src/api/release_mode.dart'; export 'src/audioplayers_platform_interface.dart'; -export 'src/global_platform_interface.dart'; +export 'src/global_audioplayers_platform_interface.dart'; export 'src/streams_interface.dart'; diff --git a/packages/audioplayers_platform_interface/lib/src/method_channel_audioplayers_platform.dart b/packages/audioplayers_platform_interface/lib/src/audioplayers_platform.dart similarity index 94% rename from packages/audioplayers_platform_interface/lib/src/method_channel_audioplayers_platform.dart rename to packages/audioplayers_platform_interface/lib/src/audioplayers_platform.dart index a5992d8af..86d782feb 100644 --- a/packages/audioplayers_platform_interface/lib/src/method_channel_audioplayers_platform.dart +++ b/packages/audioplayers_platform_interface/lib/src/audioplayers_platform.dart @@ -1,4 +1,5 @@ import 'dart:async'; + // TODO(gustl22): remove when upgrading min Flutter version to >=3.3.0 // ignore: unnecessary_import import 'dart:typed_data'; @@ -7,21 +8,21 @@ import 'package:audioplayers_platform_interface/src/api/audio_context.dart'; import 'package:audioplayers_platform_interface/src/api/player_mode.dart'; import 'package:audioplayers_platform_interface/src/api/release_mode.dart'; import 'package:audioplayers_platform_interface/src/audioplayers_platform_interface.dart'; -import 'package:audioplayers_platform_interface/src/global_platform_interface.dart'; +import 'package:audioplayers_platform_interface/src/global_audioplayers_platform_interface.dart'; import 'package:audioplayers_platform_interface/src/method_channel_interface.dart'; import 'package:audioplayers_platform_interface/src/streams_interface.dart'; import 'package:flutter/services.dart'; -class MethodChannelAudioplayersPlatform extends AudioplayersPlatform +class AudioplayersPlatform extends AudioplayersPlatformInterface with StreamsInterface { final MethodChannel _channel = const MethodChannel('xyz.luan/audioplayers'); - MethodChannelAudioplayersPlatform() { + AudioplayersPlatform() { _channel.setMethodCallHandler(platformCallHandler); } - static GlobalPlatformInterface get _logger => - GlobalPlatformInterface.instance; + static GlobalAudioplayersPlatformInterface get _logger => + GlobalAudioplayersPlatformInterface.instance; @override Future getCurrentPosition(String playerId) { diff --git a/packages/audioplayers_platform_interface/lib/src/audioplayers_platform_interface.dart b/packages/audioplayers_platform_interface/lib/src/audioplayers_platform_interface.dart index 7e9f8d097..32bbe7367 100644 --- a/packages/audioplayers_platform_interface/lib/src/audioplayers_platform_interface.dart +++ b/packages/audioplayers_platform_interface/lib/src/audioplayers_platform_interface.dart @@ -5,7 +5,7 @@ import 'package:audioplayers_platform_interface/src/api/audio_context.dart'; import 'package:audioplayers_platform_interface/src/api/for_player.dart'; import 'package:audioplayers_platform_interface/src/api/player_mode.dart'; import 'package:audioplayers_platform_interface/src/api/release_mode.dart'; -import 'package:audioplayers_platform_interface/src/method_channel_audioplayers_platform.dart'; +import 'package:audioplayers_platform_interface/src/audioplayers_platform.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; /// The interface that implementations of audioplayers must implement. @@ -15,18 +15,19 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart'; /// changes. Extending this class (using `extends`) ensures that the subclass /// will get the default implementation, while platform implementations that /// `implements` this interface will be broken by newly added -/// [AudioplayersPlatform] methods. -abstract class AudioplayersPlatform extends PlatformInterface { - AudioplayersPlatform() : super(token: _token); +/// [AudioplayersPlatformInterface] methods. +abstract class AudioplayersPlatformInterface extends PlatformInterface { + AudioplayersPlatformInterface() : super(token: _token); static final Object _token = Object(); - /// The default instance of [AudioplayersPlatform] to use. + /// The default instance of [AudioplayersPlatformInterface] to use. /// - /// Defaults to [MethodChannelAudioplayersPlatform]. + /// Defaults to [AudioplayersPlatform]. /// Platform-specific plugins should set this with their own platform-specific - /// class that extends [AudioplayersPlatform] when they register themselves. - static AudioplayersPlatform instance = MethodChannelAudioplayersPlatform(); + /// class that extends [AudioplayersPlatformInterface] when they register + /// themselves. + static AudioplayersPlatformInterface instance = AudioplayersPlatform(); /// Pauses the audio that is currently playing. /// diff --git a/packages/audioplayers_platform_interface/lib/src/global_platform_interface.dart b/packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform.dart similarity index 57% rename from packages/audioplayers_platform_interface/lib/src/global_platform_interface.dart rename to packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform.dart index 8abe69724..1f85c6953 100644 --- a/packages/audioplayers_platform_interface/lib/src/global_platform_interface.dart +++ b/packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform.dart @@ -1,30 +1,10 @@ import 'package:audioplayers_platform_interface/src/api/audio_context.dart'; import 'package:audioplayers_platform_interface/src/api/log_level.dart'; +import 'package:audioplayers_platform_interface/src/global_audioplayers_platform_interface.dart'; import 'package:audioplayers_platform_interface/src/method_channel_interface.dart'; import 'package:flutter/services.dart'; -abstract class GlobalPlatformInterface { - static GlobalPlatformInterface instance = MethodChannelGlobalPlatform(); - - LogLevel get logLevel; - - Future changeLogLevel(LogLevel value); - - Future setGlobalAudioContext(AudioContext ctx); - - void log(LogLevel level, String message) { - if (level.getLevel() <= logLevel.getLevel()) { - // ignore: avoid_print - print(message); - } - } - - void info(String message) => log(LogLevel.info, message); - - void error(String message) => log(LogLevel.error, message); -} - -class MethodChannelGlobalPlatform extends GlobalPlatformInterface { +class GlobalAudioplayersPlatform extends GlobalAudioplayersPlatformInterface { static const MethodChannel _channel = MethodChannel('xyz.luan/audioplayers.global'); diff --git a/packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform_interface.dart b/packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform_interface.dart new file mode 100644 index 000000000..4b2d6cdca --- /dev/null +++ b/packages/audioplayers_platform_interface/lib/src/global_audioplayers_platform_interface.dart @@ -0,0 +1,25 @@ +import 'package:audioplayers_platform_interface/src/api/audio_context.dart'; +import 'package:audioplayers_platform_interface/src/api/log_level.dart'; +import 'package:audioplayers_platform_interface/src/global_audioplayers_platform.dart'; + +abstract class GlobalAudioplayersPlatformInterface { + static GlobalAudioplayersPlatformInterface instance = + GlobalAudioplayersPlatform(); + + LogLevel get logLevel; + + Future changeLogLevel(LogLevel value); + + Future setGlobalAudioContext(AudioContext ctx); + + void log(LogLevel level, String message) { + if (level.getLevel() <= logLevel.getLevel()) { + // ignore: avoid_print + print(message); + } + } + + void info(String message) => log(LogLevel.info, message); + + void error(String message) => log(LogLevel.error, message); +} diff --git a/packages/audioplayers_platform_interface/test/logger_test.dart b/packages/audioplayers_platform_interface/test/logger_test.dart index 16932a268..8d87a3caa 100644 --- a/packages/audioplayers_platform_interface/test/logger_test.dart +++ b/packages/audioplayers_platform_interface/test/logger_test.dart @@ -15,7 +15,7 @@ void main() { }); final _print = OverridePrint(); - final _logger = GlobalPlatformInterface.instance; + final _logger = GlobalAudioplayersPlatformInterface.instance; group('Logger', () { setUp(_print.clear); diff --git a/packages/audioplayers_web/lib/audioplayers_web.dart b/packages/audioplayers_web/lib/audioplayers_web.dart index 5bb028f80..7f3a40277 100644 --- a/packages/audioplayers_web/lib/audioplayers_web.dart +++ b/packages/audioplayers_web/lib/audioplayers_web.dart @@ -2,16 +2,22 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart'; +import 'package:audioplayers_web/global_audioplayers_web.dart'; import 'package:audioplayers_web/num_extension.dart'; import 'package:audioplayers_web/wrapped_player.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; -class AudioplayersPlugin extends AudioplayersPlatform with StreamsInterface { +class AudioplayersPlugin with StreamsInterface { /// The entrypoint called by the generated plugin registrant. static void registerWith(Registrar registrar) { - AudioplayersPlatform.instance = AudioplayersPlugin(); + AudioplayersPlatformInterface.instance = WebAudioplayersPlatform(); + GlobalAudioplayersPlatformInterface.instance = + WebGlobalAudioplayersPlatform(); } +} +class WebAudioplayersPlatform extends AudioplayersPlatformInterface + with StreamsInterface { // players by playerId Map players = {}; diff --git a/packages/audioplayers_web/lib/global_audioplayers_web.dart b/packages/audioplayers_web/lib/global_audioplayers_web.dart new file mode 100644 index 000000000..bb56a0871 --- /dev/null +++ b/packages/audioplayers_web/lib/global_audioplayers_web.dart @@ -0,0 +1,22 @@ +import 'dart:async'; + +import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart'; + +class WebGlobalAudioplayersPlatform + extends GlobalAudioplayersPlatformInterface { + // Web implementation currently does not log anything + LogLevel _level = LogLevel.error; + + @override + Future changeLogLevel(LogLevel value) async { + _level = value; + } + + @override + LogLevel get logLevel => _level; + + @override + Future setGlobalAudioContext(AudioContext ctx) async { + // no-op: web does not support changing audio context + } +} From fdbcdfb3412d1e7022f12e6dd69e6c72108f80e4 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Mon, 20 Mar 2023 14:55:37 +0100 Subject: [PATCH 2/2] refactor!: rename GlobalAudioPlayer to GlobalAudioScope --- packages/audioplayers/example/lib/tabs/audio_context.dart | 2 +- packages/audioplayers/example/lib/tabs/logger.dart | 2 +- packages/audioplayers/lib/audioplayers.dart | 2 +- packages/audioplayers/lib/src/audioplayer.dart | 2 +- .../src/{global_audioplayer.dart => global_audio_scope.dart} | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) rename packages/audioplayers/lib/src/{global_audioplayer.dart => global_audio_scope.dart} (87%) diff --git a/packages/audioplayers/example/lib/tabs/audio_context.dart b/packages/audioplayers/example/lib/tabs/audio_context.dart index 62f1e3fca..8c1786149 100644 --- a/packages/audioplayers/example/lib/tabs/audio_context.dart +++ b/packages/audioplayers/example/lib/tabs/audio_context.dart @@ -16,7 +16,7 @@ class AudioContextTab extends StatefulWidget { class _AudioContextTabState extends State with AutomaticKeepAliveClientMixin { - static GlobalAudioPlayer get _global => AudioPlayer.global; + static GlobalAudioScope get _global => AudioPlayer.global; AudioPlayer get player => widget.player; diff --git a/packages/audioplayers/example/lib/tabs/logger.dart b/packages/audioplayers/example/lib/tabs/logger.dart index 29a5f4c5a..389f1cf53 100644 --- a/packages/audioplayers/example/lib/tabs/logger.dart +++ b/packages/audioplayers/example/lib/tabs/logger.dart @@ -11,7 +11,7 @@ class LoggerTab extends StatefulWidget { } class _LoggerTabState extends State { - static GlobalAudioPlayer get _logger => AudioPlayer.global; + static GlobalAudioScope get _logger => AudioPlayer.global; LogLevel currentLogLevel = _logger.logLevel; diff --git a/packages/audioplayers/lib/audioplayers.dart b/packages/audioplayers/lib/audioplayers.dart index 6cf1f29ef..1e2c85840 100644 --- a/packages/audioplayers/lib/audioplayers.dart +++ b/packages/audioplayers/lib/audioplayers.dart @@ -9,5 +9,5 @@ export 'package:audioplayers_platform_interface/src/api/release_mode.dart'; export 'src/audio_cache.dart'; export 'src/audio_pool.dart'; export 'src/audioplayer.dart'; -export 'src/global_audioplayer.dart'; +export 'src/global_audio_scope.dart'; export 'src/source.dart'; diff --git a/packages/audioplayers/lib/src/audioplayer.dart b/packages/audioplayers/lib/src/audioplayer.dart index fa0c5ac5e..22143fbbe 100644 --- a/packages/audioplayers/lib/src/audioplayer.dart +++ b/packages/audioplayers/lib/src/audioplayer.dart @@ -18,7 +18,7 @@ const _uuid = Uuid(); /// It holds methods to play, loop, pause, stop, seek the audio, and some useful /// hooks for handlers and callbacks. class AudioPlayer { - static final global = GlobalAudioPlayer(); + static final global = GlobalAudioScope(); static final _platform = AudioplayersPlatformInterface.instance; /// This is the [AudioCache] instance used by this player. diff --git a/packages/audioplayers/lib/src/global_audioplayer.dart b/packages/audioplayers/lib/src/global_audio_scope.dart similarity index 87% rename from packages/audioplayers/lib/src/global_audioplayer.dart rename to packages/audioplayers/lib/src/global_audio_scope.dart index f7f9295c2..fa1d071a8 100644 --- a/packages/audioplayers/lib/src/global_audioplayer.dart +++ b/packages/audioplayers/lib/src/global_audio_scope.dart @@ -1,7 +1,7 @@ import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart'; -/// Handle Global calls and events concerning all AudioPlayers. -class GlobalAudioPlayer { +/// Handle global audio scope like calls and events concerning all AudioPlayers. +class GlobalAudioScope { static final _platform = GlobalAudioplayersPlatformInterface.instance; LogLevel get logLevel => _platform.logLevel;