Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dispose player implementation #1470

Merged
merged 5 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/audioplayers/example/integration_test/lib_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ void main() {
);
});

group('Platform method channel', () {
testWidgets('#create and #dispose', (tester) async {
final platform = AudioplayersPlatformInterface.instance;

const playerId = 'somePlayerId';
await platform.create(playerId);
await tester.pumpAndSettle();
await platform.dispose(playerId);
});
});

group('Logging', () {
testWidgets('Emit platform log', (tester) async {
final completer = Completer<String>();
Expand Down
3 changes: 1 addition & 2 deletions packages/audioplayers/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class _ExampleAppState extends State<ExampleApp> {
case PopupAction.remove:
setState(() {
if (audioPlayers.isNotEmpty) {
selectedAudioPlayer.stop();
selectedAudioPlayer.release();
selectedAudioPlayer.dispose();
audioPlayers.removeAt(selectedPlayerIdx);
}
// Adjust index to be in valid range
Expand Down
6 changes: 3 additions & 3 deletions packages/audioplayers/lib/src/audioplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class AudioPlayer {
/// The resources are going to be fetched or buffered again as soon as you
/// call [resume] or change the source.
Future<void> release() async {
await creatingCompleter.future;
await stop();
await _platform.release(playerId);
state = PlayerState.stopped;
_source = null;
Expand Down Expand Up @@ -350,8 +350,9 @@ class AudioPlayer {
// First stop and release all native resources.
await release();

await _platform.dispose(playerId);

final futures = <Future>[
creatingCompleter.future,
if (!_playerStateController.isClosed) _playerStateController.close(),
_onPlayerCompleteStreamSubscription.cancel(),
_onLogStreamSubscription.cancel(),
Expand All @@ -362,6 +363,5 @@ class AudioPlayer {
_source = null;

await Future.wait<dynamic>(futures);
await _platform.dispose(playerId);
}
}
8 changes: 5 additions & 3 deletions packages/audioplayers/test/audioplayers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ void main() {
expect(player.source, null);
});

test('#setSource', () async {
test('#setSource and #dispose', () async {
await player.setSource(UrlSource('internet.com/file.mp3'));
expect(platform.popLastCall().method, 'setSourceUrl');
expect(player.source, isInstanceOf<UrlSource>());
final urlSource = player.source as UrlSource?;
expect(urlSource?.url, 'internet.com/file.mp3');

await player.release();
expect(platform.popLastCall().method, 'release');
await player.dispose();
expect(platform.popCall().method, 'stop');
expect(platform.popCall().method, 'release');
expect(platform.popLastCall().method, 'dispose');
expect(player.source, null);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
val player = getPlayer(playerId)
try {
when (call.method) {
"dispose" -> {
player.dispose()
players.remove(playerId)
}

"setSourceUrl" -> {
val url = call.argument<String>("url") ?: error("url is required")
val isLocal = call.argument<Boolean>("isLocal") ?: false
Expand Down Expand Up @@ -152,10 +147,10 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
player.volume = volume.toFloat()
}

"setBalance" -> {
val balance = call.argument<Double>("balance") ?: error("balance is required")
player.balance = balance.toFloat()
}
"setBalance" -> {
val balance = call.argument<Double>("balance") ?: error("balance is required")
player.balance = balance.toFloat()
}

"setPlaybackRate" -> {
val rate = call.argument<Double>("playbackRate") ?: error("playbackRate is required")
Expand Down Expand Up @@ -198,6 +193,11 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
player.handleError(code, message, null)
}

"dispose" -> {
player.dispose()
players.remove(playerId)
}

else -> {
response.notImplemented()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ public class SwiftAudioplayersDarwinPlugin: NSObject, FlutterPlugin {
return
}
player.eventHandler.onError(code: code, message: message, details: nil)
} else if method == "dispose" {
player.dispose()
players[playerId] = nil
} else {
result(FlutterMethodNotImplemented)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ static void audioplayers_linux_plugin_handle_method_call(
flMessage == nullptr ? "" : fl_value_get_string(flMessage);
player->OnError(code, message, nullptr, nullptr);
result = 1;
} else if (strcmp(method, "dispose") == 0) {
player->Dispose();
audioPlayers.erase(playerId);
result = 1;
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
fl_method_call_respond(method_call, response, nullptr);
Expand Down
7 changes: 3 additions & 4 deletions packages/audioplayers_web/lib/audioplayers_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ class WebAudioplayersPlatform extends AudioplayersPlatformInterface {

@override
Future<void> dispose(String playerId) async {
await Future.forEach<WrappedPlayer>(
players.values,
(player) => player.dispose(),
);
final player = getPlayer(playerId);
await player.dispose();
players.remove(playerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ void AudioplayersWindowsPlugin::HandleMethodCall(
auto message = GetArgument<std::string>("message", args, std::string());
player->OnError(code, message, nullptr);
result->Success(EncodableValue(1));
} else if (method_call.method_name().compare("dispose") == 0) {
player->Dispose();
audioPlayers.erase(playerId);
result->Success(EncodableValue(1));
} else {
result->NotImplemented();
}
Expand Down