I am basically trying to call different songs whenever the sensor detects a change in movement for example...
If no movement is detected play song 1. If movement is detected play song 2. Else if movement stops play song 1 again.
So far, I have been successful and where I am now plays through the first two If statements above, however, I can't get it back to song 1 without the music player playing over itself. I've tried using .pause();
but it only seems to pause for a second and plays song1 over itself again and again.
This is what I have so far:
public void onSensorChanged(SensorEvent se) {
float x = se.values[0];
float y = se.values[1];
float z = se.values[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta; // perform low-cut filter
mp1.start(); //song1 starts
if (mAccel > 5) { // movement
mp2.start(); // start song2
if (mp2.isPlaying()) { // if song 2 is playing stop song1
mp1.stop();
}
}
}
Use 1 media player object, not 2 of them. By re-using the media player you will ensure that only 1 sound will be played at once whilst also keeping system resource usage low.
Create a new function that will stop the media player and then release it like:
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
Then add a function to re-create and start the media player going with the new audio.