Skip to content

[BUG] Android SoundPlayer not working along firebase_messaging #61

Closed

Description

@diegogarciar

Sounds Version :

sounds: ^1.0.0-beta13


Severity

  • Result is not what expected
  • Minor issue

Platforms you faced the error

  • Android

Logs

(This is very important. Most of the time we cannot do anything if we do not have information on your bug)


flutter doctor :

[✓] Flutter (Channel stable, 1.22.5, on macOS 11.1 20C69 darwin-x64, locale en-MX)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.3)
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.52.1)
[✓] Connected device (3 available)


flutter pub deps :

just firebase_messaging, here is a repo of a project replicating the issue
https://github.com/markusaksli-nc/sounds-messaging-issue


Screenshots


Describe the bug
I submitted this bug on firebase a while ago but I think it is also worth it to mention it here. Theres a firebase_messaging function to receive background notifications and when the function is registered, the communication from Android to Dart stops working in this plugin. I honestly don't know which package to blame, but I think a lot of us might use this combination of plugins and the bug took me hours to detect that could save time to others.

The issue is well described in the original firebase issue. so I'll keep it short here

To Reproduce
Steps to reproduce the behavior:

  1. Register onBackgroundMessage function from firebase_messaging
  2. Play audio file
  3. See error

Expected behavior
A clear and concise description of what you expected to happen.
The progress indicator would not move, it seems that android is indeed sending updates of the progress but dart is not receiving them. After commenting the registration of the background message, the progress indicators works as expected.


Additional context**

I did some research on their plugin and it seems that they are creating a new FlutterEngine on that function, maybe this plugin (or all plugins) are confusing it with the original FlutterEngine?

Activity

  1. bsutton commented on Jan 14, 2021

    @bsutton
    Owner
  2. diegogarciar commented on Jan 14, 2021

    @diegogarciar
    Author

    Yes its strange, I checked the plugin's android implementation and it is not doing something different from other plugins. From what I learned, each flutterEngine has its own thread, so it shouldn't interfere.

  3. bsutton commented on Jan 15, 2021

    @bsutton
    Owner

    So I think I've found the problem.

    In the SoundPlayer and recorder we have:

    @UiThread
    	private void sendUpdateProgress(MediaPlayer mp) {
    		try {
    			JSONObject json = new JSONObject();
    			json.put("duration", String.valueOf(mp.getDuration()));
    			json.put("current_position", String.valueOf(mp.getCurrentPosition()));
    			getPlugin().invokeCallbackWithString(slotNo, "updateProgress", json.toString());
    
    			// reschedule ourselves.
    			tickHandler.postDelayed(() -> sendUpdateProgress(mp), (model.progressInterval));
    		} catch (Exception e) {
    			Log.d(TAG, "Exception: " + e.toString());
    		}
    	}

    I had assumed that magically @UiThread made this method run on the main thread. But that was just lazy thinking :<

    So the problem is when we allocate the tick handler:

    	final private Handler tickHandler = new Handler();

    This needs to be changed to:

    	final private Handler tickHandler = new Handler(Looper.getMainLooper());

    The same goes for the SoundRecorder.

  4. diegogarciar commented on Jan 15, 2021

    @diegogarciar
    Author

    Awesome! I’m glad you were able to find the bug. Will you be submitting an update?

  5. bsutton commented on Jan 15, 2021

    @bsutton
    Owner
  6. bsutton commented on Mar 18, 2021

    @bsutton
    Owner

    Fixed in 1.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions