Hi I am trying to make a service that keeps the microphone muted in the background on a galaxys6 the service is running as the toasts show and all state the mic is muted the problem is when I launch google voice search it pauses my service and turns the mic on. How can I prevent this from happening and only allow my app to stop or pause the service.
public class blockservice extends Service {
AudioManager audioManager;
// #Nullable
#Override
public void onCreate() {
super.onCreate();
audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//audioManager.setMode(AudioManager.MODE_IN_CALL);
// audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setMicrophoneMute(true);
new Thread(new Runnable() {
public void run(){
for(int i=0;i<10;i++){
postToastMessage("boom "+i+" "+audioManager.isMicrophoneMute());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
audioManager.setMicrophoneMute(false);
super.onDestroy();
}
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
Related
My application uses the camera, it displays the camera preview, and on every preview frame it encodes the frame data to base64 form to call my remote web service.
Since the speed of producing frame doesn't match the speed of consuming frame(calling web service), I consider to use a buffer(message queue) in my service, so I use Messenger to bind the service.
Therefore, I consider to use the Service, and bind it to my CameraActivity. By default the service runs in the same thread as the UI thread, however, I want to run the service in another thread. I've read the Service and the Bound service, they just mention that I can run the service in other thread but don't give an example.
CameraActivity
public class CameraActivity extends AppCompatActivity implements Camera.PreviewCallback {
private Camera camera;
private CameraPreview preview;
int cnt = 0, RATE = 0;
private Messenger messenger;//use to communicate with CloudService
private boolean bound = false;
private ServiceConnection connection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
messenger = new Messenger(service);
bound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
messenger = null;
bound = false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
camera = getCamera();
RATE = camera.getParameters().getPreviewFrameRate() / 5;
preview = new CameraPreview(this, camera);
FrameLayout layout = findViewById(R.id.camera_preview);
layout.addView(preview);
bindService(new Intent(this, CloudService.class), connection,
Context.BIND_AUTO_CREATE);
}
public static Camera getCamera() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
return camera;
}
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (cnt == RATE) {
if (bound) {
Message message = Message.obtain(null, 0, data);
try {
messenger.send(message);
Log.d("activity", "send msg");
} catch (RemoteException e) {
e.printStackTrace();
}
}
cnt = 0;
}
cnt++;
}
protected void onStop() {
super.onStop();
if (bound){
unbindService(connection);
bound = false;
}
}
}
CloudService
private Thread thread;
private Messenger messenger;
public CloudService() {
Log.d("cloud service", "create");
thread = new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("cloud service", "thread running");
}
}
});
}
#Override
public IBinder onBind(Intent intent) {
messenger = new Messenger(new PreviewHandler(this));
return messenger.getBinder();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("cloud service", "start");
thread.start();
return START_STICKY;
}
#Override
public void onDestroy() {
thread.interrupt();
Log.d("cloud service", "destroy");
}
static class PreviewHandler extends Handler {
private CloudService service;
public PreviewHandler(CloudService service) {
this.service = service;
}
#Override
public void handleMessage(Message message) {
byte[] data = (byte[]) message.obj;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("message", "msg!!!");
}
}
}
How can I use thread in my CloudService properly?
Solution: Using HandlerThread and Handler together to achieve your task.
public class CloudService extends Service {
private static final String TAG = "CloudService";
private HandlerThread handlerThread;
private PreviewHandler handler;
private Messenger messenger;
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
handlerThread = new HandlerThread("CloudService_HandlerThread");
handlerThread.start();
handler = new PreviewHandler(handlerThread.getLooper());
}
#Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
messenger = new Messenger(handler);
return messenger.getBinder();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return START_STICKY;
}
#Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
handlerThread.quit();
}
static class PreviewHandler extends Handler {
PreviewHandler(Looper looper) {
super(looper);
}
#Override
public void handleMessage(Message message) {
byte[] data = (byte[]) message.obj;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "handleMessage from thread " + Thread.currentThread().getName());
}
}
}
I am trying to play an audio using service and update seekbar item in adapter. But I am unable to get duration of audio from service. Also when scrolling recyclerview, more than one item view is updated instead of updating currently playing seekbar item. I have tried below code.
public class LocalService extends Service {
boolean mBound = false;
private final IBinder mBinder = new LocalBinder();
private String path = "";
private int timer;
private static MediaPlayer mediaPlayer = new MediaPlayer();
// Random number generator
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/**
* method for clients
*/
public int getTotalTime() {
return mediaPlayer.getDuration();
}
public int getDuration() {
// Log.v("player duration",timer+"");
try {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
Log.v("playing", (mediaPlayer.getCurrentPosition() / 1000) + "");
return mediaPlayer.getCurrentPosition() / 1000;
} else {
//Log.v("not playing", (mediaPlayer.getCurrentPosition() / 1000) + "");
return 0;
}
}catch (IllegalStateException i)
{
i.printStackTrace();
return 0;
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
#Override
public void onCreate() {
super.onCreate();
try {
mediaPlayer = new MediaPlayer();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setAudio(String path)
{
try {
mediaPlayer.setDataSource(path);
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void playAudio() {
Log.v("audio path", path);
try {
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.prepareAsync();
mediaPlayer.start();
timer=mediaPlayer.getDuration();
Log.v("total timer1",timer+"");
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
timer=0;
mediaPlayer.stop();
mediaPlayer.reset();
//mediaPlayer.release();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void unbindService(ServiceConnection conn) {
super.unbindService(conn);
mediaPlayer.reset();
mediaPlayer.release();
}
#Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.reset();
mediaPlayer.release();
}
Service is started from adapter constructor.
Intent intent = new Intent(mContext, LocalService.class);
mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
player item click in recycler adpater
vhItemHolder.playPauseImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
currentlyPlayingPosition = position;
if (mBound) {
Log.v("service","bound");
//Make sure you update Seekbar on UI thread
mService.setAudio(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer=new MediaPlayer();
try {
mPlayer.setDataSource(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer.prepareAsync();
vhItemHolder.seekBar.setMax(mPlayer.getDuration());
} catch (IOException e) {
e.printStackTrace();
}
mService.playAudio();
Log.v("duration",(mService.getTotalTime())+"");
vhItemHolder.seekBar.setMax(mService.getTotalTime());
mHandler.removeCallbacks(null);
mHandler=new Handler();
((DoctorActivity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
num = mService.getDuration();
Log.v("progress",num+"");
vhItemHolder.seekBar.setProgress((num));
vhItemHolder.runningTimerTextView.setText(convertMilliToMinutes((long)mService.getDuration()));
mHandler.postDelayed(this, 50);
}
});
}
else
{
Log.v("service","not bound");
}
Inside your adapter class:
You are making a new object of MediaPlayer (mPlayer) in viewHolder's playPauseImageView's onClickListener and calling setDataSource() method on it which sets data source for this local mPlayer object not to the mediaPlayer object of your LocalService class.
Calling mService.playAudio() and mService.getDuration() methods access your LocalService class' mediaPlayer object for which setDataSource() method is not called yet, hence must be returning wrong duration and unable to play.
You should make a change in your adapter class like below:
vhItemHolder.playPauseImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
currentlyPlayingPosition = position;
if (mBound) {
Log.v("service","bound");
//Make sure you update Seekbar on UI thread
mService.setAudio(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer = mService.mediaPlayer;// or mPlayer = LocalService.mediaPlayer; as being static object.
//rather new MediaPlayer();
try {
mPlayer.setDataSource(audioDirectory+messageArrayList.get(position).getmMediaValue());
mPlayer.prepareAsync();
vhItemHolder.seekBar.setMax(mPlayer.getDuration());
} catch (IOException e) {
e.printStackTrace();
}
mService.playAudio();
Log.v("duration",(mService.getTotalTime())+"");
vhItemHolder.seekBar.setMax(mService.getTotalTime());
mHandler.removeCallbacks(null);
mHandler=new Handler();
((DoctorActivity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
num = mService.getDuration();
Log.v("progress",num+"");
vhItemHolder.seekBar.setProgress((num));
vhItemHolder.runningTimerTextView.setText(convertMilliToMinutes((long)mService.getDuration()));
mHandler.postDelayed(this, 50);
}
});
}
else
{
Log.v("service","not bound");
}
I am developing an audio streaming app for android devices and I have some issues about the handling of background audio service.
The expected behaviour of my app is that the audio source will be played continuously, even if the device is in stand-by mode with the screen off, but sometimes the background audio service is interrupted without any apparent reason.
To fix this problem, I have started the service with the option START_STICKY.
In this way, the service will be restarted even if it is closed for any reason, but now I can't close the service with a force close of the app and sometimes the service it will be restarted even if I want to close it!
Does anyone can suggest me a correct way to handle this job?
Here is my code:
At first I have declared the intent in the MainActivity:
Intent playbackServiceIntent;
Inside the onCreate() method of the MainActivity I have registered the service:
playbackServiceIntent = new Intent(this, BackgroundAudioService.class);
To start the audio service through the play button:
startService(playbackServiceIntent);
To stop the service through the stop button
stopService(playbackServiceIntent);
I have alse overrided the onDestroy() method of the MainActivity:
#Override
public void onDestroy() {
stopService(playbackServiceIntent);
//stopService(new Intent(this,BackgroundAudioService.class));
super.onDestroy();
}
Finally, the BackgroundAudioService class is defined as follow:
public class BackgroundAudioService extends Service implements OnCompletionListener {
public MediaPlayer mediaPlayer;
public WifiLock wifiLock;
PhoneStateListener mPhoneStateListener;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
mediaPlayer = new MediaPlayer(); // raw/s.mp3
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
wifiLock.acquire();
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
}
});
mPhoneStateListener = new PhoneStateListener()
{
protected boolean mWasPlayingWhenCalled = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
if( state == TelephonyManager.CALL_STATE_RINGING )
{ // Incoming call: Pause music
mediaPlayer.pause();
mWasPlayingWhenCalled = true;
}
else if(state == TelephonyManager.CALL_STATE_IDLE )
{ // Not in call: Play music
if( mWasPlayingWhenCalled )
{
mediaPlayer.start();
mWasPlayingWhenCalled = false;
}
}
else if( state == TelephonyManager.CALL_STATE_OFFHOOK )
{ // A call is dialing, active or on hold
mediaPlayer.pause();
mWasPlayingWhenCalled = true;
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
mgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
Intent intent = new Intent();
intent.setAction("com.my_app.CUSTOM_INTENT");
sendBroadcast(intent);
}
});
Time t=new Time();
t.setToNow();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
wifiLock.release();
mediaPlayer.release();
TelephonyManager mgr1 = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr1 != null) {
mgr1.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
Time t=new Time();
t.setToNow();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
Time t=new Time();
t.setToNow();
}
}
I building a Radio Streaming app and i am having difficulties sending my MediaPlayer object from my service to to a fragment. Below is my Service and my Fragment code. When i try to run the code it's always crashing
public class StreamService extends Service implements
MediaPlayer.OnCompletionListener,
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnBufferingUpdateListener {
private static final int NOTIFICATION_ID = 1;
private PhoneStateListener phoneStateListener;
private TelephonyManager telephonyManager;
private boolean isPausedInCall = false;
private NotificationCompat.Builder builder;
//intent
private Intent bufferIntent;
public static final MediaPlayer mediaPlayer = new MediaPlayer();
ScheduledExecutorService scheduleTaskExecutor, reminderTaskExecutor;
private ScheduleModel scheduleModel;
private int currentHour;
private String currentDay, notificationTitle;
private final IBinder mBinder = new LocalBinder();
#Override
public void onCreate() {
super.onCreate();
bufferIntent = new Intent(BROADCAST_BUFFER);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.reset();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
reminderTaskExecutor = Executors.newScheduledThreadPool(5);
reminderTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
//initNotification();
}
}, 0, 1, TimeUnit.MINUTES);
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
case TelephonyManager.CALL_STATE_RINGING:
if (mediaPlayer != null) {
pauseMedia();
isPausedInCall = true;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (mediaPlayer != null) {
if (isPausedInCall) {
isPausedInCall = false;
playMedia();
}
}
break;
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
initNotification();
mediaPlayer.reset();
/**
* play media
*/
if (!mediaPlayer.isPlaying()) {
try {
mediaPlayer.setDataSource(URL_STREAM);
// sent to UI radio is buffer
sendBufferingBroadcast();
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
Log.d("error", e.getMessage());
} catch (IllegalStateException e) {
Log.d("error", e.getMessage());
} catch (IOException e) {
Log.d("error", e.getMessage());
}
}
return START_STICKY;
}
public class LocalBinder extends Binder {
public StreamService getService() {
// Return this instance of LocalService so clients can call public methods
return StreamService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
#Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopMedia();
stopSelf();
}
#Override
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Toast.makeText(this, "Error not valid playback", Toast.LENGTH_SHORT).show();
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Toast.makeText(this, "Error server died", Toast.LENGTH_SHORT).show();
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
Toast.makeText(this, "Error occurred, please try again", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// sent to UI, audio has buffered
sendBufferCompleteBroadcast();
playMedia();
}
private void pauseMedia() {
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
}
private void playMedia() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
private void stopMedia() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
}
/**
* sent buffering
*/
private void sendBufferingBroadcast() {
bufferIntent.putExtra("buffering", "1");
sendBroadcast(bufferIntent);
}
/**
* sent buffering complete
*/
private void sendBufferCompleteBroadcast() {
bufferIntent.putExtra("buffering", "0");
sendBroadcast(bufferIntent);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("tag", "remove notification");
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
if (phoneStateListener != null) {
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
cancelNotification();
}
}
My Fragment, code crashed on line mVisualizerView.link(StreamService.mediaPlayer);
private void startVisualizerView(){
mVisualizerView = (VisualizerView) getView().findViewById(R.id.visualizerView);
mVisualizerView.link(StreamService.mediaPlayer);
// Start with just line renderer
addLineRenderer();
}
private void startVisualizerView(){
mVisualizerView = (VisualizerView) getView().findViewById(R.id.visualizerView);
mVisualizerView.link(StreamService.mediaPlayer);
// Start with just line renderer
addLineRenderer();
}
private void startStreaming(Context context) {
stopStreaming();
try {
getActivity().startService(serviceIntent);
Toast.makeText(context, "Enjoy #Hashtag Radio...", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
}
private void stopStreaming() {
try {
getActivity().stopService(serviceIntent);
// reset streaming tag
isStreaming = false;
Utils.setDataBooleanToSP(getActivity(), Utils.IS_STREAM, false);
} catch (Exception e) {
}
}
private void startStreaming(Context context) {
stopStreaming();
try {
getActivity().startService(serviceIntent);
startVisualizerView();
Toast.makeText(context, "Enjoy #Hashtag Radio...", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
StreamService.LocalBinder binder = (StreamService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
I have a class which calls this service class to play the media player
class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.s);// raw/s.mp3
mediaPlayer.setOnCompletionListener(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
I don't want it to load the raw file i want it to load a path which a string in the class that calls this service.
String path = "path is here"
private void playAudio(String url) throws Exception{
Intent music = new Intent(this,MusicService.class);
intent.putExtra("paths",path)
startService(music);
How would i go about doing this...
The music file comes from the sd card
Simply use this---------
MediaPlayer md=new MediaPlayer();
md.prepare();
md.setDataSource(path);
I don't know what are you doing in your activity but, If your are just passing a music file path in service and then want to start media player in service then I think you have to try something like this,
MediaPlayer mediaPlayer;
String musicFile;
#Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
#Override
onStartCommand(Intent intent, int flags, int startId) {
musicFile=intent.getExtra("paths");
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(musicFile);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!mediaPlayer.isPlaying())
{
mediaPlayer.start();
}
return START_STICKY;
}