package com.test.android.MusicCom;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.Binder;
import anywheresoftware.b4a.BA.ShortName;
@ShortName("MusicCom")
public class MusicCom{
protected static final String TAG = "MusicCom";
private boolean isHtc;
private MediaPlayerServiceConnection musicConn;
/**
* Selects the next song
*
* @throws RemoteException
*
*/
public void Next() {
try {
musicConn.nextSong();
} catch (RemoteException e) {
}
}
/**
* Selects the Previous song
*
* @throws RemoteException
*/
public void Prev() {
try {
musicConn.prevSong();
} catch (RemoteException e) {
}
}
/**
* Get the name of the current Trackname
*
* @return
* @throws RemoteException
*/
public String TrackName() {
try {
return musicConn.getTrackName();
} catch (RemoteException e) {
return "error";
}
}
public void initialise() {
Intent i = new Intent();
musicConn = new MediaPlayerServiceConnection();
isHtc = true;
i.setClassName("com.htc.music", "com.htc.music.MediaPlaybackService");
if (!this.bindService(i, musicConn, Context.MODE_PRIVATE)) {
isHtc = false;
i.setClassName("com.android.music", "com.android.music.MediaPlaybackService");
this.bindService(i, musicConn, Context.MODE_PRIVATE);
}
}
/**
* 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;
public void onServiceConnected(ComponentName name, IBinder service) {
// 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();
}
}
/**
* Get the name of the current Trackname
*
* @return
* @throws RemoteException
*/
public String getTrackName() throws RemoteException {
if (isHtc) {
return mServiceHtc.getTrackName();
} else {
return mServiceAndroid.getTrackName();
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
}
}