Add disposePlayer to web implementation.

This commit is contained in:
Ryan Heise 2020-10-11 03:38:01 +11:00
parent bbbfdf3a4d
commit ab1a4612b3
4 changed files with 37 additions and 18 deletions

View File

@ -1,3 +1,7 @@
## 0.1.0
* Update to use platform interface 1.1.0.
## 0.0.1 ## 0.0.1
* Migrated to the federated plugin model. * Migrated to the federated plugin model.

View File

@ -10,28 +10,37 @@ import 'package:just_audio_platform_interface/just_audio_platform_interface.dart
final Random _random = Random(); final Random _random = Random();
class JustAudioPlugin extends JustAudioPlatform { class JustAudioPlugin extends JustAudioPlatform {
final Map<String, JustAudioPlayer> players = {};
static void registerWith(Registrar registrar) { static void registerWith(Registrar registrar) {
print("registerWith setting instance");
JustAudioPlatform.instance = JustAudioPlugin(); JustAudioPlatform.instance = JustAudioPlugin();
} }
Future<AudioPlayerPlatform> init(InitRequest request) async { Future<AudioPlayerPlatform> init(InitRequest request) async {
return Html5AudioPlayer(id: request.id); final player = Html5AudioPlayer(id: request.id);
players[request.id] = player;
return player;
}
Future<DisposePlayerResponse> disposePlayer(
DisposePlayerRequest request) async {
await players[request.id]?.release();
return DisposePlayerResponse();
} }
} }
abstract class JustAudioPlayer extends AudioPlayerPlatform { abstract class JustAudioPlayer extends AudioPlayerPlatform {
final String id;
final eventController = StreamController<PlaybackEventMessage>(); final eventController = StreamController<PlaybackEventMessage>();
ProcessingStateMessage _processingState = ProcessingStateMessage.none; ProcessingStateMessage _processingState = ProcessingStateMessage.none;
bool _playing = false; bool _playing = false;
int _index; int _index;
JustAudioPlayer({@required this.id}); JustAudioPlayer({@required String id}) : super(id);
@mustCallSuper @mustCallSuper
Future<DisposeResponse> dispose(DisposeRequest request) async { Future<void> release() async {
eventController.close(); eventController.close();
return DisposeResponse();
} }
Duration getCurrentPosition(); Duration getCurrentPosition();
@ -170,6 +179,7 @@ class Html5AudioPlayer extends JustAudioPlayer {
@override @override
Future<LoadResponse> load(LoadRequest request) async { Future<LoadResponse> load(LoadRequest request) async {
print("web load");
_currentAudioSourcePlayer?.pause(); _currentAudioSourcePlayer?.pause();
_audioSourcePlayer = getAudioSource(request.audioSourceMessage); _audioSourcePlayer = getAudioSource(request.audioSourceMessage);
_index = 0; _index = 0;
@ -180,6 +190,7 @@ class Html5AudioPlayer extends JustAudioPlayer {
} }
Future<Duration> loadUri(final Uri uri) async { Future<Duration> loadUri(final Uri uri) async {
print("loadUri $uri");
transition(ProcessingStateMessage.loading); transition(ProcessingStateMessage.loading);
final src = uri.toString(); final src = uri.toString();
if (src != _audioElement.src) { if (src != _audioElement.src) {
@ -198,6 +209,7 @@ class Html5AudioPlayer extends JustAudioPlayer {
} }
transition(ProcessingStateMessage.ready); transition(ProcessingStateMessage.ready);
final seconds = _audioElement.duration; final seconds = _audioElement.duration;
print("loadUri returning");
return seconds.isFinite return seconds.isFinite
? Duration(milliseconds: (seconds * 1000).toInt()) ? Duration(milliseconds: (seconds * 1000).toInt())
: null; : null;
@ -349,12 +361,13 @@ class Html5AudioPlayer extends JustAudioPlayer {
Duration getDuration() => _currentAudioSourcePlayer?.duration; Duration getDuration() => _currentAudioSourcePlayer?.duration;
@override @override
Future<DisposeResponse> dispose(DisposeRequest request) async { Future<void> release() async {
print("web release");
_currentAudioSourcePlayer?.pause(); _currentAudioSourcePlayer?.pause();
_audioElement.removeAttribute('src'); _audioElement.removeAttribute('src');
_audioElement.load(); _audioElement.load();
transition(ProcessingStateMessage.none); transition(ProcessingStateMessage.none);
return await super.dispose(request); return await super.release();
} }
List<AudioSourcePlayer> getAudioSources(List messages) => List<AudioSourcePlayer> getAudioSources(List messages) =>

View File

@ -7,14 +7,14 @@ packages:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.0" version: "1.1.0-nullsafety.3"
collection: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.14.13" version: "1.15.0-nullsafety.3"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -28,9 +28,9 @@ packages:
just_audio_platform_interface: just_audio_platform_interface:
dependency: "direct main" dependency: "direct main"
description: description:
name: just_audio_platform_interface path: "../just_audio_platform_interface"
url: "https://pub.dartlang.org" relative: true
source: hosted source: path
version: "1.0.0" version: "1.0.0"
meta: meta:
dependency: "direct main" dependency: "direct main"
@ -38,7 +38,7 @@ packages:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.8" version: "1.3.0-nullsafety.3"
plugin_platform_interface: plugin_platform_interface:
dependency: transitive dependency: transitive
description: description:
@ -57,14 +57,14 @@ packages:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0" version: "1.3.0-nullsafety.3"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.8" version: "2.1.0-nullsafety.3"
sdks: sdks:
dart: ">=2.9.0-14.0.dev <3.0.0" dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.12.13+hotfix.5" flutter: ">=1.12.13+hotfix.5"

View File

@ -1,7 +1,7 @@
name: just_audio_web name: just_audio_web
description: Web platform implementation of just_audio description: Web platform implementation of just_audio
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_web homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_web
version: 0.0.1 version: 0.1.0
flutter: flutter:
plugin: plugin:
@ -11,7 +11,9 @@ flutter:
fileName: just_audio_web.dart fileName: just_audio_web.dart
dependencies: dependencies:
just_audio_platform_interface: ^1.0.0 # just_audio_platform_interface: ^1.0.0
just_audio_platform_interface:
path: ../just_audio_platform_interface
flutter: flutter:
sdk: flutter sdk: flutter
flutter_web_plugins: flutter_web_plugins: