I have an activity that is a service which plays audio using a media player. This is the service
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();
}
}
Instead of playing from that raw file i want it to play from a path that is a String that comes from the sd card. the path is held in the class that calls the service how do i pass that string to the service class and put it in the mediaplayer create method. here is where i call the service
private void playAudio(String url) throws Exception{
Intent music = new Intent(this,MusicService.class);
startService(music);
}
Use the putExtra commands of the starting Intent:
Then get that passed info in the onStartCommand of your mediaplayer service:
Beware that this intent can be null in cases when the service is restarted by the system.