Im trying to make soundpool work because I need to use it for a prodject. The problem is that it’s not playing any sound. Im just trying to play a test sound to see if it works. Hope someone can see what im doing wrong here.
public class MainActivity extends AppCompatActivity {
SoundPool soundPool;
int game_over;
@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(4)
.setAudioAttributes(audioAttributes)
.build();
}
game_over = soundPool.load(this, R.raw.test, 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(game_over, 1, 1, 1, 1, 1f);
}
});
}
}
2
Answers
This code is now working. Why it was not playing on my phone was because it was using the system volume and not the music volume. Hope this will help someone else in the future.
set up
setOnLoadCompleteListener
BEFOREload
call (which should usesampleId
, asgame_over
may be not assigned yet)afaik all
load
methods ofSoundPool
are synchronous – they are returning ID of already loaded audio file. settingOnLoadCompleteListener
AFTER loading audio will cause no call for this listener, as there is nothing more to load