Skip to content

Instantly share code, notes, and snippets.

@sudar
Created March 5, 2011 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudar/856293 to your computer and use it in GitHub Desktop.
Save sudar/856293 to your computer and use it in GitHub Desktop.
Finding the currently playing song in Android
/**
* The service connection class that allows you to talk to the MediaPlayer Service
*
* @author "Sudar Muthu (http://sudarmuthu.com)"
*
*/
private class MediaPlayerServiceConnection implements ServiceConnection {
public com.htc.music.IMediaPlaybackService mServiceHtc;
public com.android.music.IMediaPlaybackService mServiceAndroid;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MediaPlayerServiceConnection", "Connected! Name: " + name.getClassName());
// This is the important line where we bind the service
if (isHtc)
mServiceHtc = com.htc.music.IMediaPlaybackService.Stub.asInterface(service);
else
mServiceAndroid = com.android.music.IMediaPlaybackService.Stub.asInterface(service);
}
/**
* Selects the next song
*
* @throws RemoteException
*
*/
public void nextSong() throws RemoteException {
if (isHtc) {
mServiceHtc.next();
} else {
mServiceAndroid.next();
}
}
/**
* Selects the Previous song
*
* @throws RemoteException
*/
public void prevSong() throws RemoteException {
if (isHtc) {
mServiceHtc.prev();
} else {
mServiceAndroid.prev();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("MediaPlayerServiceConnection", "Disconnected!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment