FANDOM




Using a Service to control the MediaPlayerEdit

OnCompletionListener


public void onDestroy() {

  if (mediaPlayer.isPlaying())
  {
    mediaPlayer.stop();
  }

  mediaPlayer.release();

  Log.v("SIMPLESERVICE","onDestroy");

}



In order to have a button in an Activity interact with a Service, we have to bind to the Service like so;

if (v == startPlaybackButton) {
  startService(playbackServiceIntent);
  bindService(
    playbackServiceIntent, 
    serviceConnection,
    Context.BIND_AUTO_CREATE
  );
} else if (v == stopPlaybackButton) {
  unbindService(serviceConnection);
  stopService(playbackServiceIntent);
}




mediaPlayer.setOnCompletionListener(this);

mediaPlayer.setOnErrorListener(this);

mediaPlayer.setOnInfoListener(this);

mediaPlayer.setOnPreparedListener(this);

mediaPlayer.setOnSeekCompleteListener(this);

mediaPlayer.setOnVideoSizeChangedListener(this);