package ru.vinyarsky.androidaudioexample.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import ru.vinyarsky.androidaudioexample.R;
import ru.vinyarsky.androidaudioexample.service.PlayerService;

public class MainActivity extends AppCompatActivity {

    PlayerService.PlayerServiceBinder playerServiceBinder;
    MediaControllerCompat mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button playButton = (Button) findViewById(R.id.play);
        final Button pauseButton = (Button) findViewById(R.id.pause);
        final Button stopButton = (Button) findViewById(R.id.stop);
        final Button skipToNextButton = (Button) findViewById(R.id.skip_to_next);
        final Button skipToPreviousButton = (Button) findViewById(R.id.skip_to_previous);

        bindService(new Intent(this, PlayerService.class), new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                playerServiceBinder = (PlayerService.PlayerServiceBinder) service;
                try {
                    mediaController = new MediaControllerCompat(MainActivity.this, playerServiceBinder.getMediaSessionToken());
                    mediaController.registerCallback(new MediaControllerCompat.Callback() {
                        @Override
                        public void onPlaybackStateChanged(PlaybackStateCompat state) {
                            if (state == null)
                                return;
                            boolean playing = state.getState() == PlaybackStateCompat.STATE_PLAYING;
                            playButton.setEnabled(!playing);
                            pauseButton.setEnabled(playing);
                            stopButton.setEnabled(playing);
                        }
                    });
                }
                catch (RemoteException e) {
                    mediaController = null;
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                playerServiceBinder = null;
                mediaController = null;
            }
        }, BIND_AUTO_CREATE);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaController != null)
                    mediaController.getTransportControls().play();
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaController != null)
                    mediaController.getTransportControls().pause();
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaController != null)
                    mediaController.getTransportControls().stop();
            }
        });

        skipToNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaController != null)
                    mediaController.getTransportControls().skipToNext();
            }
        });

        skipToPreviousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaController != null)
                    mediaController.getTransportControls().skipToPrevious();
            }
        });
    }
}

@@ -634,16 +634,24 @@
  %7B%0A%0A    
+private 
 PlayerSe
@@ -701,16 +701,24 @@
 er;%0A    
+private 
 MediaCon
@@ -747,16 +747,118 @@
 troller;
+%0A    private MediaControllerCompat.Callback callback;%0A    private ServiceConnection serviceConnection;
 %0A%0A    @O
@@ -1406,58 +1406,507 @@
     
-bindService(new Intent(this, PlayerService.class),
+callback = new MediaControllerCompat.Callback() %7B%0A            @Override%0A            public void onPlaybackStateChanged(PlaybackStateCompat state) %7B%0A                if (state == null)%0A                    return;%0A                boolean playing = state.getState() == PlaybackStateCompat.STATE_PLAYING;%0A                playButton.setEnabled(!playing);%0A                pauseButton.setEnabled(playing);%0A                stopButton.setEnabled(playing);%0A            %7D%0A        %7D;%0A%0A        serviceConnection =
  new
@@ -2322,80 +2322,18 @@
 ack(
-new MediaControllerCompat.Callback() %7B%0A                        @Override
+callback);
 %0A   
@@ -2349,32 +2349,25 @@
         
-    public void 
+callback.
 onPlayba
@@ -2385,454 +2385,42 @@
 ged(
-PlaybackStateCompat state) %7B%0A                            if (state == null)%0A                                return;%0A                            boolean playing = state.getState() == PlaybackStateCompat.STATE_PLAYING;%0A                            playButton.setEnabled(!playing);%0A                            pauseButton.setEnabled(playing);%0A                            stopButton.setEnabled(playing);%0A                        %7D%0A                    %7D
+mediaController.getPlaybackState()
 );%0A 
@@ -2699,32 +2699,36 @@
                 
+if (
 mediaController 
@@ -2731,39 +2731,249 @@
 ler 
+!
 = null
-;%0A            %7D%0A        %7D
+) %7B%0A                    mediaController.unregisterCallback(callback);%0A                    mediaController = null;%0A                %7D%0A            %7D%0A        %7D;%0A%0A        bindService(new Intent(this, PlayerService.class), serviceConnection
 , BI
@@ -4378,11 +4378,313 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onDestroy() %7B%0A        super.onDestroy();%0A        playerServiceBinder = null;%0A        if (mediaController != null) %7B%0A            mediaController.unregisterCallback(callback);%0A            mediaController = null;%0A        %7D%0A        unbindService(serviceConnection);%0A    %7D
 %0A%7D%0A

fix #4 No memory leaks please