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

Sounds plugin's MethodChannel will loose connection to the underlying FlutterEngine #46

Conversation

mrducnguyen
Copy link
Contributor

I recognised that I'll need to cross-post this issue on Flutter repo also, while I was still creating this Pull Request. Please keep it here just for a record. Better wait for Flutter team to response

I ran into an issue with Sounds when I used Android Alarm Manager plugin

  • Whenever an Isolate is spawn up in Android from Android Alarm Manager, Sounds would loose connection to the Main dart thread, hence all subsequence invokeMethod calling from Android won't be executed on the main app

How to reproduce, for instance, assume that we need to use AndroidAlarmManager for our Flutter app (it's actually thanks to that plugin that I found this bug)

  • Add dependency to Android Alarm Manager
  • Initialise the plugin by calling await AndroidAlarmManager.initialize();
  • As soon as AndroidAlarmManager is initialised, Sounds plugin will not be able to communicate with Dart code anymore, all invokeMethod calling from Android still being executed, but the Dart side won't be reached.

I've spent some time to debug and found out that the method below of the main plugin class

public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding)

will be called whenever a FlutterEngine got created, so it will triggered all onAttachedToEngine on the Plugins registry (the auto generated GeneratedPluginRegistrant). This will cause Sounds to attach to the newly created background FlutterEngine.

I have made some changes by following what have been done in Android Alarm Manager in handling this. I have tried to research the documents but doesn't seem like the Flutter doc mentioned anything relating to this. So, I'm not 100% sure this fix should be implemented on Sounds plugin or on the FlutterEngine itself...

PS: Android Studio automatically removed unused imports, I didn't recognise it until I were looking at the changed files below. It's a good change though I reckon?!

…o attach to another FlutterEngine thus loose connection to the current Dart main thread
@mrducnguyen
Copy link
Contributor Author

Needs to spend more time to investigate, the problem was when Alarm Android Manager background service started, the updateProgress callback didn't work, but play/pause still works.

More over, the only line of code which seems to make everything works was the checking for Null of SoundPlayerPlugin.channel. I have no clue when channel is created. As attachSoundPlayer is only called in Sounds onAttachedToEngine`

		if (SoundPlayerPlugin.channel != null) {
			// already attached to a FlutterEngine
			return;
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant