I'm using the BackgroundSoundService for playing music in a background of my activity. However, when I switch to another activity, I would like to use another sound. This is how my class looks like. I need to change that "R.raw.sound" programmatically.
public class BackgroundSoundService extends Service {
MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.sound);
mediaPlayer.setLooping(true); // Set looping
mediaPlayer.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer.start();
return startId;
}
@Override
public void onDestroy() {
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public void onLowMemory() {
}}