I am having a couple problems I could really use some help on both. The first is I am trying to get an audio file to play when the application is started and as soon as the shake occurs it stops. (It is not playing it at all on either screen it is supposed to). The other issue I am having is when I shake the phone an audio is supposed to play. It is doing just that, but the problem is that at the end of the audio playing it make a pop sound. That noise isn’t on the audio files so I am not exactly sure where that sound is coming from. Any help would be appreciated. Thanks in advance.
public class Ask extends Activity{
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
String[] answer;
int possibleAnswers, randomAnswer, talkRun=0;
long lastClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask);
final Random generator = new Random();
//Sounds
final SoundPool sounds = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
final int sound0 = sounds.load(this, R.raw.coughing, 1);
final int sound25 = sounds.load(this, R.raw.askbud, 1);
sounds.play(sound25, 1f, 1f, 1, 0, 1f);
mSensorListener = new ShakeEventListener();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
final TextView tv = (TextView)findViewById(R.id.answer);
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
public void onShake() {
if (System.currentTimeMillis() - lastClick > 500) {
lastClick = System.currentTimeMillis();
sounds.stop(sound25);
sounds.stop(sound0);
randomAnswer = generator.nextInt(possibleAnswers);
if(randomAnswer==0){
sounds.play(sound0, 1f, 1f, 1, 0, 1f);
}
}
}
});
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}
}
I fixed it by switching from using sound pool to using media player. Also got to make sure to release() or it was causing other problems like the sound stopped playing.