Add disposePlayer to platform interface. Retain player.position after dispose.

This commit is contained in:
Ryan Heise 2020-10-10 15:46:14 +11:00
parent 35a6e4810b
commit ad9e4518cf
11 changed files with 87 additions and 97 deletions

View file

@ -41,6 +41,11 @@ abstract class JustAudioPlatform extends PlatformInterface {
Future<AudioPlayerPlatform> init(InitRequest request) {
throw UnimplementedError('init() has not been implemented.');
}
/// Disposes of a platform player.
Future<DisposePlayerResponse> disposePlayer(DisposePlayerRequest request) {
throw UnimplementedError('disposePlayer() has not been implemented.');
}
}
/// A nested platform interface for communicating with a particular player
@ -53,6 +58,10 @@ abstract class JustAudioPlatform extends PlatformInterface {
/// `implements` this interface will be broken by newly added
/// [AudioPlayerPlatform] methods.
abstract class AudioPlayerPlatform {
final String id;
AudioPlayerPlatform(this.id);
Stream<PlaybackEventMessage> get playbackEventMessageStream {
throw UnimplementedError(
'playbackEventMessageStream has not been implemented.');
@ -103,6 +112,9 @@ abstract class AudioPlayerPlatform {
"setAndroidAudioAttributes() has not been implemented.");
}
/// This method has been superceded by [JustAudioPlatform.disposePlayer].
/// For backward compatibility, this method will still be called as a
/// fallback if [JustAudioPlatform.disposePlayer] is not implemented.
Future<DisposeResponse> dispose(DisposeRequest request) {
throw UnimplementedError("dispose() has not been implemented.");
}
@ -244,6 +256,21 @@ class InitRequest {
};
}
class DisposePlayerRequest {
final String id;
DisposePlayerRequest({@required this.id});
Map<dynamic, dynamic> toMap() => {
'id': id,
};
}
class DisposePlayerResponse {
static DisposePlayerResponse fromMap(Map<dynamic, dynamic> map) =>
DisposePlayerResponse();
}
class LoadRequest {
final AudioSourceMessage audioSourceMessage;

View file

@ -13,15 +13,22 @@ class MethodChannelJustAudio extends JustAudioPlatform {
await _mainChannel.invokeMethod('init', request.toMap());
return MethodChannelAudioPlayer(request.id);
}
@override
Future<DisposePlayerResponse> disposePlayer(
DisposePlayerRequest request) async {
return DisposePlayerResponse.fromMap(
await _mainChannel.invokeMethod('disposePlayer', request.toMap()));
}
}
/// An implementation of [AudioPlayerPlatform] that uses method channels.
class MethodChannelAudioPlayer extends AudioPlayerPlatform {
final String id;
final MethodChannel _channel;
MethodChannelAudioPlayer(this.id)
: _channel = MethodChannel('com.ryanheise.just_audio.methods.$id');
MethodChannelAudioPlayer(String id)
: _channel = MethodChannel('com.ryanheise.just_audio.methods.$id'),
super(id);
@override
Stream<PlaybackEventMessage> get playbackEventMessageStream =>