0

I am working on a Android Streaming App using MediaBrowserServiceCompat based on this great article https://medium.com/androiddevelopers/mediabrowserservicecompat-and-the-modern-media-playback-app-7959a5196d90. My app works pretty good except when phone goes to Doze Mode. I have been looking over internet and see some possible solutions to keep my service alive. One of those is running the UI and the Music Service in different Process. However since I am using the MediaBrowserService I don't know how to start the service from UI using AIDL.

This is how I connected the service with the Main Activity in the same process:

MainActivity.java

 private class MediaBrowserConnection extends MediaBrowserHelper {
    private MediaBrowserConnection(Context context) {
        super(context, MusicService.class);
    }

    @Override
    protected void onConnected(@NonNull MediaControllerCompat mediaController) {
        mMediaController = mediaController;
    }

    @Override
    protected void onChildrenLoaded(@NonNull String parentId,
                                    @NonNull List<MediaBrowserCompat.MediaItem> children) {
        super.onChildrenLoaded(parentId, children);
    }
}

MediaBrowserHelper.java

public MediaBrowserHelper(Context context,
                          Class<? extends MediaBrowserServiceCompat> serviceClass) {
    mContext = context;
    mMediaBrowserServiceClass = serviceClass;

    mMediaBrowserConnectionCallback = new MediaBrowserConnectionCallback();
    mMediaControllerCallback = new MediaControllerCallback();
    mMediaBrowserSubscriptionCallback = new MediaBrowserSubscriptionCallback();
}

public void onStart() {
    if (mMediaBrowser == null) {
        mMediaBrowser =
                new MediaBrowserCompat(
                        mContext,
                        new ComponentName(mContext, mMediaBrowserServiceClass),
                        mMediaBrowserConnectionCallback,
                        null);
        mMediaBrowser.connect();
    }
    Log.d(TAG, "onStart: Creating MediaBrowser, and connecting");
}

public void onStop() {
    if (mMediaController != null) {
        mMediaController.unregisterCallback(mMediaControllerCallback);
        mMediaController = null;
    }
    if (mMediaBrowser != null && mMediaBrowser.isConnected()) {
        mMediaBrowser.disconnect();
        mMediaBrowser = null;
    }
    resetState();
    Log.d(TAG, "onStop: Releasing MediaController, Disconnecting from MediaBrowser");
}

Anyone knows how to do this or is there any other reliable solution to keep streaming alive?

Thanks

limaho33
  • 25
  • 7
  • When your app is playing, it should be a foreground service as per [the documentation](https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice#mediastyle-notifications). Foreground services aren't affected by doze, so what exactly is your problem? – ianhanniballake Mar 09 '20 at 04:18
  • Thanks for your answer. Yes I am using foreground services to stream media links. However when phone is lock after 5 minutes, music is stopped. I am using partial wake locks and foreground services. – limaho33 Mar 09 '20 at 13:34

0 Answers0