I created a simple app where the user can click on a button and a sound is played. Everything works well except when I click HOME then return to the app the button doesn't work anymore, no sound is fired. When I return to the app, the onResume method is called and mSoundPool is not null, but I don't know why the sound is not fired until I quit the app with the BACK button then return.
public class MainActivity extends Activity {
ImageButton btn_word, btn_play;
TextView tv_text;
TextView tv_time, tv_title;
String[] words;
private MyCountDownTimer mycountDownTimer;
private boolean timerHasStarted = false;
private int mStream = 1, mStream2 = 2, mStream3 = 3;
float streamVolume;
AudioManager mAudioManager;
int maxStreamNumber=5; //the maximum number of simultaneous streams for this SoundPool object
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
words = getResources().getStringArray(R.array.szavak);
btn_play = (ImageButton)findViewById(R.id.btn_2);
btn_word = (ImageButton)findViewById(R.id.btn_1);
tv_text = (TextView)findViewById(R.id.tv_text);
tv_time = (TextView)findViewById(R.id.tv_time);
tv_title = (TextView)findViewById(R.id.tv_title);
tv_title.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/colonnamt.otf"));
tv_text.setText("Új szó");
tv_time.setText("0:59");
btn_play.setImageResource(R.drawable.btn_play);
mycountDownTimer = new MyCountDownTimer(59100, 1000);
mSoundPool = new SoundPool(maxStreamNumber, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mSoundPoolMap = new HashMap<Integer, Integer>();
mSoundPoolMap.put(mStream, mSoundPool.load(this, R.raw.szovaltas, 1));
mSoundPoolMap.put(mStream2, mSoundPool.load(this, R.raw.rovid, 2));
mSoundPoolMap.put(mStream3, mSoundPool.load(this, R.raw.hosszu, 3));
streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 2.5f;
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!timerHasStarted) {
mycountDownTimer.start();
timerHasStarted = true;
btn_play.setImageResource(R.drawable.btn_stop);
mSoundPool.play(mSoundPoolMap.get(mStream2), streamVolume, streamVolume, 1, 0, 1f);
}
else {
mycountDownTimer.cancel();
timerHasStarted = false;
btn_play.setImageResource(R.drawable.btn_play);
tv_time.setText("0:59");
}
}
});
btn_word.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String randomStr = words[new Random().nextInt(words.length)];
tv_text.setText(randomStr);
mSoundPool.play(mSoundPoolMap.get(mStream), streamVolume, streamVolume, 1, 0, 1f);
}
});
}
@Override
public void onResume() {
if (mSoundPool != null) {
Log.i("RESUME", "not null"); //this is logged out after returning to app
mSoundPool.resume(mStream);
} else {
Log.i("RESUME", "null");
}
super.onResume();
}
@Override
public void onPause() {
super.onPause();
Log.i("STATUS", "onPause");
if (mSoundPool != null) mSoundPool.release();
}
@Override
public void onStop() {
super.onStop();
Log.i("STATUS", "onStop");
if (mSoundPool != null) mSoundPool.release();
}
@Override
public void onDestroy() {
Log.i("STATUS", "onDestroy");
super.onDestroy();
if (mSoundPool != null) mSoundPool.release();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
public void onFinish() {
tv_time.setText("0:59");
btn_play.setImageResource(R.drawable.btn_play);
mSoundPool.play(mSoundPoolMap.get(mStream3 ), streamVolume, streamVolume, 1, 0, 1f);
}
public void onTick(long millisUntilFinished) {
tv_time.setText("0:" + placeZero(millisUntilFinished / 1000));
}
}
public String placeZero(long s) {
String c = "";
if (s < 10) {
c = "0" + String.valueOf(s);
} else {
c = String.valueOf(s);
}
return c;
}
}
Calling
mSoundPool.resume(mStream);
mSoundPool.resume(mStream2);
mSoundPool.resume(mStream3);
in the onResume() does not work.
It's probably because you release() your soundpool in onPause() and onStop() which are both called when pushing the home button. Try soundpool.pause();