Using BackgroundAudioServiceBinder
package app.test; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; class BackgroundAudioService extends Service implements OnCompletionListener { MediaPlayer mediaPlayer; public class BackgroundAudioServiceBinder extends Binder { BackgroundAudioService getService() { return BackgroundAudioService.this; } } private final IBinder basBinder = new BackgroundAudioServiceBinder(); @Override public IBinder onBind(Intent intent) { return basBinder; } public void haveFun() { if (mediaPlayer.isPlaying()) { mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - 2500); } } @Override public void onCreate() { mediaPlayer = MediaPlayer.create(this, R.raw.test);// raw/test.mp3 mediaPlayer.setOnCompletionListener(this); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v("PLAYERSERVICE", "onStartCommand"); if (!mediaPlayer.isPlaying()) { mediaPlayer.start(); } return START_STICKY; } public void onDestroy() { if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } mediaPlayer.release(); Log.v("SIMPLESERVICE", "onDestroy"); } public void onCompletion(MediaPlayer _mediaPlayer) { stopSelf(); } } public class Test extends Activity implements OnClickListener { private BackgroundAudioService baService; Button startPlaybackButton, stopPlaybackButton, haveFunButton; Intent playbackServiceIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startPlaybackButton = (Button) this .findViewById(R.id.StartPlaybackButton); stopPlaybackButton = (Button) this .findViewById(R.id.StopPlaybackButton); haveFunButton = (Button) this.findViewById(R.id.HaveFunButton); startPlaybackButton.setOnClickListener(this); stopPlaybackButton.setOnClickListener(this); haveFunButton.setOnClickListener(this); playbackServiceIntent = new Intent(this, BackgroundAudioService.class); } public void onClick(View v) { if (v == startPlaybackButton) { startService(playbackServiceIntent); bindService(playbackServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE); } else if (v == stopPlaybackButton) { unbindService(serviceConnection); stopService(playbackServiceIntent); } else if (v == haveFunButton) { baService.haveFun(); } } private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder baBinder) { baService = ((BackgroundAudioService.BackgroundAudioServiceBinder) baBinder) .getService(); } public void onServiceDisconnected(ComponentName className) { baService = null; } }; } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Background Audio Player" /> <Button android:text="Start Playback" android:id="@+id/StartPlaybackButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Stop Playback" android:id="@+id/StopPlaybackButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Have Fun" android:id="@+id/HaveFunButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>