I wrote a class which will handle the MediaPlaybackService in android. But the code is not working.
My code for binding the serviceconnection is as follows:
Intent in = new Intent();
in.setClassName("com.android.playerapps","com.android.playerapps.MediaPlayerServiceConnection");
ServiceConnection conn = new MediaPlayerServiceConnection();
ctx.bindService(in, conn, 0);
ServiceConnection class is as follows:
public class MediaPlayerServiceConnection implements ServiceConnection {
IMediaPlaybackService mService;
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MediaPlayerServiceConnection", "Connected! Name: " +name.getClassName());
// This is the important line
mService = IMediaPlaybackService.Stub.asInterface(service);
// Process org.videolan.vlc.android with component org.videolan.vlc.android.AudioService
System.out.println( "---------------- track:");
// If all went well, now we can use the interface
try {
if (mService.isPlaying())
{
//String str=getTrackName();
//String str=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/"+mService.getPath();
System.out.println( "Playing track: "+this.getTrackName());
Log.i("MediaPlayerServiceConnection", "Music player is playing.");
}
else
{
Log.i("MediaPlayerServiceConnection", "Music player is not playing.");
}
}
catch (Exception e) {
e.printStackTrace();
//throw new RuntimeException(e);
}
}
public void onServiceDisconnected(ComponentName name) {
Log.i("MediaPlayerServiceConnection", "Disconnected!");
}
public String getTrackName() {
try {
return ( mService.getTrackName() );
} catch (RemoteException e) {
Log.e("MediaConnection", "Failed to get TrackName");
return "Failed to get TrackName";
}
}
Plz help!!