java - Play song if sensor detects certain amount of movement -
i trying call different songs whenever sensor detects change in movement example...
if no movement detected play song 1. if movement detected play song 2. else if movement stops play song 1 again.
so far, have been successful , plays through first 2 if statements above, however, can't song 1 without music player playing on itself. i've tried using .pause();
seems pause second , plays song1 on again , again.
this have 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 playing stop song1 mp1.stop(); } } }
use 1 media player object, not 2 of them. re-using media player ensure 1 sound played @ once whilst keeping system resource usage low.
create new function stop media player , release like:
private void stopplaying() { if (mp != null) { mp.stop(); mp.release(); mp = null; } }
then add function re-create , start media player going new audio.
Comments
Post a Comment