title: WallpaperService keeps music playing in background although app is closed
body: <p>in my app i want to offer the user a option to set an animation as live wallpaper using <code>WallpaeprService</code>, also my app include a Radio player (playing in the backgroud) if app is open so you can navigate in other apps while music is playing. </p>
  
  <p><strong>my problem :</strong> </p>
  
  <p>if <em>live wallpaper</em> is working and user try to close the app by (Swipe to exit / Recent Task),the music keeps playing although app is closed.</p>
  
  <p>i tried to stop music like this ,but doesn't work : </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  
      @SuppressLint({"CommitPrefEdits", "Assert"})
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          startService(new Intent(this, KillNotificationService.class));
          trackSelector = new DefaultTrackSelector();
          loadControl = new DefaultLoadControl();
          exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getApplicationContext()), trackSelector, loadControl);
          MusicButton.setVisibility(View.INVISIBLE);
          MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (Build.VERSION.SDK_INT &gt; 15) {
                      if (started &amp;&amp; MusicButton.isChecked()) {
                          started = false;
                          exoPlayer.setPlayWhenReady(false);
                          MusicButton.setChecked(true);
                          releaseAudioFocusForMyApp(MainActivity.this);
                      } else {
                          boolean gotFocus = requestAudioFocusForMyApp(MainActivity.this);
                          if (gotFocus) {
                              started = true;
                              exoPlayer.setPlayWhenReady(true);
                              MusicButton.setChecked(false);
                          }
                      }
                  } else {
                      if (started &amp;&amp; MusicButton.isChecked()) {
                          started = false;
                          mediaPlayer.pause();
                          MusicButton.setChecked(true);
                          releaseAudioFocusForMyApp(MainActivity.this);
                      } else {
                          boolean gotFocus = requestAudioFocusForMyApp(MainActivity.this);
                          if (gotFocus) {
                              started = true;
                              mediaPlayer.start();
                              MusicButton.setChecked(false);
                          }
                      }
                  }
              }
          });
  
  
  
      private void playRadio(String url) {
          Uri audioUri = Uri.parse(url);
          DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("ExoPlayerDemo");
          ExtractorsFactory extractor = new DefaultExtractorsFactory();
          MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory).setExtractorsFactory(extractor).createMediaSource(audioUri);
          exoPlayer.prepare(audioSource);
          prepared = true;
          exoPlayer.setPlayWhenReady(true);
          exoPlayer.addListener(new Player.EventListener() {
              @Override
              public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {
  
              }
  
              @Override
              public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
  
              }
  
              @Override
              public void onLoadingChanged(boolean isLoading) {
  
              }
  
              @Override
              public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                  if (isPreparing &amp;&amp; playbackState == ExoPlayer.STATE_READY) {
                      MusicButton.setVisibility(View.VISIBLE);
                      MusicButton.setChecked(true);
                      isPreparing = false;
                      isReady = true;
                  }
  
              }
  
              @Override
              public void onRepeatModeChanged(int repeatMode) {
  
              }
  
              @Override
              public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
  
              }
  
              @Override
              public void onPlayerError(ExoPlaybackException error) {
  
              }
  
              @Override
              public void onPositionDiscontinuity(int reason) {
  
              }
  
              @Override
              public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
  
              }
  
              @Override
              public void onSeekProcessed() {
  
              }
          });
  
      }
  
  
  
      @Override
      protected void onResume() {
          super.onResume();
          try {
              MApplication.sBus.register(this);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      protected void onStop() {
          super.onStop();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          try {
              MApplication.sBus.unregister(this);
          } catch (Exception e) {
              e.printStackTrace();
          }
          MApplication.sBus.post(PlaybackEvent.PAUSE);
      }
  
      @Override
      protected void onStart() {
          super.onStart();
      }
  
      @Subscribe
      public void handlePlaybackEvent(PlaybackEvent event) {
          switch (event) {
              case PAUSE:
                  if (Build.VERSION.SDK_INT &gt; 15) {
                      if (exoPlayer.getPlayWhenReady()) {
                          exoPlayer.setPlayWhenReady(false);
                          MusicButton.setChecked(true);
                      }
                  } else {
                      if (mediaPlayer.isPlaying()) {
                          mediaPlayer.pause();
                          MusicButton.setChecked(true);
                      }
                  }
  
                  break;
              case PLAY:
                  if (Build.VERSION.SDK_INT &gt; 15) {
                      if (!exoPlayer.getPlayWhenReady()) {
                          exoPlayer.setPlayWhenReady(true);
                          MusicButton.setChecked(false);
                      }
                  } else {
                      if (!mediaPlayer.isPlaying()) {
                          mediaPlayer.start();
                          MusicButton.setChecked(false);
                      }
                  }
  
                  break;
          }
      }
  
  
      @Override
      public void onPause() {
          super.onPause();
      }
  
      }
  }
  </code></pre>