I set up a SoundPool for playing a sound at regular intervals but it is not playing any audio. I have tried MediaPlayer, but that eventually crashed the app, so I'd like to know if the way I'm doing it now is even a good way of doing it? I am still using a Media Player for the intro sound, but that one works fine. I am very new to Java and Android Studio.
I'm planning to make the program pick randomly from a set of sounds when it is time to play (that's why randomWithRange and the list of other sounds is there), but for now, I'm just trying to get the sound slow1 to play before I go any further.
public class MainActivity extends AppCompatActivity implements LocationListener {
private Handler mHandler = new Handler();
private float nCurrentSpeed = 0;
private SoundPool soundPool;
private int fast1, fast2, fast3, fast4, fast5, fast7, fast8, fast9, fast10, thatsastart, therewego, slow1, slow2, slow3, slow4, slow5, slow6, slow7, slow8, slow9, slow10, stops1, stops2, stops3, stops4, stops5, stops6, stops7, stops8, stops9, stops10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setMaxStreams(32)
.setAudioAttributes(audioAttributes)
.build();
} else {
soundPool = new SoundPool(32, AudioManager.STREAM_MUSIC, 0);
}
fast1 = soundPool.load(this, R.raw.fast1, 1);
fast2 = soundPool.load(this, R.raw.fast2, 1);
fast3 = soundPool.load(this, R.raw.fast3, 1);
fast4 = soundPool.load(this, R.raw.fast4, 1);
fast5 = soundPool.load(this, R.raw.fast5, 1);
fast7 = soundPool.load(this, R.raw.fast7, 1);
fast8 = soundPool.load(this, R.raw.fast8, 1);
fast9 = soundPool.load(this, R.raw.fast9, 1);
fast10 = soundPool.load(this, R.raw.fast10, 1);
thatsastart = soundPool.load(this, R.raw.thatsastart, 1);
therewego = soundPool.load(this, R.raw.therewego, 1);
slow1 = soundPool.load(this, R.raw.slow1, 1);
slow2 = soundPool.load(this, R.raw.slow2, 1);
slow3 = soundPool.load(this, R.raw.slow3, 1);
slow4 = soundPool.load(this, R.raw.slow4, 1);
slow5 = soundPool.load(this, R.raw.slow5, 1);
slow6 = soundPool.load(this, R.raw.slow6, 1);
slow7 = soundPool.load(this, R.raw.slow7, 1);
slow8 = soundPool.load(this, R.raw.slow8, 1);
slow9 = soundPool.load(this, R.raw.slow9, 1);
slow10 = soundPool.load(this, R.raw.slow10, 1);
stops1 = soundPool.load(this, R.raw.stops1, 1);
stops2 = soundPool.load(this, R.raw.stops2, 1);
stops3 = soundPool.load(this, R.raw.stops3, 1);
stops4 = soundPool.load(this, R.raw.stops4, 1);
stops5 = soundPool.load(this, R.raw.stops5, 1);
stops6 = soundPool.load(this, R.raw.stops6, 1);
stops7 = soundPool.load(this, R.raw.stops7, 1);
stops8 = soundPool.load(this, R.raw.stops8, 1);
stops9 = soundPool.load(this, R.raw.stops9, 1);
stops10 = soundPool.load(this, R.raw.stops10, 1);
final android.media.MediaPlayer introSound = MediaPlayer.create(this, R.raw.intro);
final android.widget.Button playIntro = (android.widget.Button) this.findViewById(R.id.button3);
playIntro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this MediaPlayer is used only when you press a button at the start of the program and it works fine
introSound.start();
mHandler.postDelayed(mToastRunnable, 10000);
playIntro.setVisibility(View.GONE);
}
});
}
int randomWithRange(int min, int max){
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}
private Runnable mToastRunnable = new Runnable(){
@Override
public void run(){
//the toast shows up at regular intervals like I want it to, but not the sound
Toast.makeText(MainActivity.this,"should play sound", Toast.LENGTH_SHORT).show();
soundPool.play(slow1, 1, 1, 0, 0, 1);
mHandler.postDelayed(this, 3000);
}
};
// I tried with and without OnDestroy
@Override
protected void onDestroy(){
super.onDestroy();
soundPool.release();
soundPool = null;
}
}
I added this code right after all the "soundPool.load"s to see if it loads in time, and the toast shows up long before the sound is supposed to be played, so it seems like it loads in time, but the toast doesn't go away for some reason.
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Toast.makeText(MainActivity.this,"done loading", Toast.LENGTH_SHORT).show();
//when I try to play a sound from right here, it doesn't work either
}
});
I don't know if that's worth mentioning.