1

I try to create some music application with help android-UniversalMusicPlayer sample project. For that i create service in type MediaBrowserServiceCompat and register callback in MediaSessionCompat. after all in my activity, i can PAUSE or PLAY music but STOP does not work.

Actually stopself() dose not stop service.

Service:

public class MediaPlayerService extends MediaBrowserServiceCompat implements MediaPlayer.OnCompletionListener,AudioManager.OnAudioFocusChangeListener{
    .....
     private MediaSessionCompat.Callback mMediaSessionCallback = new MediaSessionCompat.Callback() {

        ....

        @Override
        public void onStop() {
          super.onStop();
          stopSelf();//problem
        }
      };
    }

Activity

public class HomeActivity extends AppCompatActivity{
    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MediaControllerCompat.getMediaController(HomeActivity.this).getTransportControls().stop();
            }
        });
    }
}
Saman
  • 2,506
  • 4
  • 22
  • 41
  • `stopForeground(true)` in addition? – Vlad May 17 '18 at 03:15
  • @V.Kalyuzhnyu.Dose not work – Saman May 17 '18 at 07:47
  • I guess there is still a bind to the service active. As reported in the documentation :The onStop() callback should call stopSelf(). If the service was started, this stops it. In addition, the service is destroyed if there are no activities bound to it. Otherwise, the service remains bound until all its activities unbind. (If a subsequent startService() call is received before the service is destroyed, the pending stop is cancelled.) Source : https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice – Pecana Sep 27 '18 at 10:13

0 Answers0