How does Capacitor on Android actually work?

@rapropos You’re pretty knowledgable, you got any insights on this one?!

I’m currently building an app that plays audio using the MediaPlayer in Android (Java). This is all working and I’m not askinf for help with this part at all.

I’ve noticed that the app out of the box:

  • still plays my audio when I put my app in the background
  • still plays audio when I lock the phone screen.
  • never times out (tried with an 18m mp3)

Questions:

  1. Is the Android version of the Capacitor bridge effectively already running as a thread or foreground service and that’s how it communicates with JS?

  2. So I’m wondering if I need to specifically write a background service to encapsulate my mediaPlayer or not still?

Would really like to understand this before I dive any further.

Just for example, this is a stub of my plugin:

e.g.

@CapacitorPlugin(name = "RemoteAudio")
public class RemoteAudioPlugin extends Plugin {

    final MediaPlayer mp = new MediaPlayer();

    @PluginMethod
    public void play(PluginCall call) {
      mp.play();
    }

    @PluginMethod
    public void pause(PluginCall call) {
      mp.pause();
    }

    // etc...
}
1 Like

@Daveshirman Did you ever find your answer? I am also interested in figuring out how to use MediaPlayer with ionic capacitor.

What do you mean? No one answered this question about capacitor.

It’s literally like no one from the core team actually cares enough to relate with devs.

If you’re asking about MediaPlayer, I ditched it and wrote my own plugin that uses ExoPlayer and a foreground service - which handles the app being put to the background and still playing music as per the Android documentation.

1 Like